jquery实现文本框textarea自适应高度_javascript技巧

浏览器中默认的文本框是不能根据内容的增多变高,只能固定高度有滚动条,体验不是很好,找了很多方法兼容都不行,总算找到个兼容良好的方法:

<body>
    <textarea id="textarea3" style="overflow-y:hidden; height:20px;resize: none">

    </textarea>
    <script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>
    <script type="text/javascript">
      $(function() {
        //最小高度和最大高度默认
        $("#textarea1").textareaAutoHeight();
        //最大高度为100px
        $("#textarea2").textareaAutoHeight({maxHeight: 100});
        //最小高度为50px,最大高度为200px
        $("#textarea3").textareaAutoHeight({minHeight: 50, maxHeight: 200});
      })

      $.fn.extend({
        textareaAutoHeight: function(options) {
          this._options = {
            minHeight: 0,
            maxHeight: 1000
          }

          this.init = function() {
            for (var p in options) {
              this._options[p] = options[p];
            }
            if (this._options.minHeight == 0) {
              this._options.minHeight = parseFloat($(this).height());
            }
            for (var p in this._options) {
              if ($(this).attr(p) == null) {
                $(this).attr(p, this._options[p]);
              }
            }
            $(this).keyup(this.resetHeight).change(this.resetHeight)
                .focus(this.resetHeight);
          }
          this.resetHeight = function() {
            var _minHeight = parseFloat($(this).attr("minHeight"));
            var _maxHeight = parseFloat($(this).attr("maxHeight"));

            if (!$.browser.msie) {
              $(this).height(0);
            }
            var h = parseFloat(this.scrollHeight);
            h = h < _minHeight ? _minHeight :h > _maxHeight ? _maxHeight : h;
            $(this).height(h).scrollTop(h);
            if (h >= _maxHeight) {
              $(this).css("overflow-y", "scroll");
            }
            else {
              $(this).css("overflow-y", "hidden");
            }
          }
          this.init();
        }
      });
    </script>
  </body>

以上就是本文的全部内容,希望对大家学习jquery程序设计有所帮助。

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索jquery
textarea自适应高度
textarea高度自适应、textarea大小自适应、textarea 宽度自适应、让textarea高度自适应、textarea 自适应,以便于您获取更多的相关知识。

时间: 2024-08-16 16:15:03

jquery实现文本框textarea自适应高度_javascript技巧的相关文章

js实现文本框选中的方法_javascript技巧

本文实例讲述了js实现文本框选中的方法.分享给大家供大家参考.具体如下: 这段javascript代码可解决文本框获得焦点,即使得文本框的内容被选中. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www

JavaScript监听文本框回车事件并过滤文本框空格的方法_javascript技巧

本文实例讲述了JavaScript监听文本框回车事件并过滤文本框空格的方法.分享给大家供大家参考.具体如下: <script type="text/javascript" language="javascript"> var username = null; var password = null; //获取文本框 onload = function() { username = document.getElementById("txtUser

js实时监听文本框状态的方法_javascript技巧

复制代码 代码如下: <div id="msg"></div> <input type="text" name="txt" id="txt"/> <script> //当状态改变的时候执行的函数 function handle() { document.getElementById('msg').innerHTML = document.getElementById('txt')

如何实现iframe(嵌入式帧)的自适应高度_javascript技巧

好几次看到有人提问问到如何实现 iframe 的自适应高度,能够随着页面的长度自动的适应以免除页面和 iframe 同时出现滚动条的现象,刚好我在工作中也碰到了类似问题,于是上网翻查,东抄抄西看看,弄出来这么一个函数,贴到页面里面就能用了.不敢独享,大家要是觉得有用,欢迎使用 源代码如下 复制代码 代码如下: <script type="text/javascript">  //** iframe自动适应页面 **//  //输入你希望根据页面高度自动调整高度的iframe

使用javascript实现Iframe自适应高度_javascript技巧

方法一: 复制代码 代码如下: $(window.parent.document).find("#ContentIframe").load(function() {                     var main = $(window.parent.document).find("#ContentIframe");                     var thisheight = $(document).height();             

Textarea根据内容自适应高度_javascript技巧

直接看代码吧,很简单,也很实用. 复制代码 代码如下: <!DOCTYPE html><html><head><title>autoresizing textarea</title><style type="text/css">textarea {    border: 0 none white;    overflow: hidden;    padding: 0;    outline: none;    ba

javascript 装载iframe子页面,自适应高度_javascript技巧

假设主页面有一个div,里面放置一个iframe 复制代码 代码如下: <div id="frameBox"> <iframe id="frameWin" src="1.html" name="opWin" style="width:100%; height:100% " frameborder="0" scrolling="no"></

php 批量添加多行文本框textarea一行一个_php技巧

复制代码 代码如下: $act=!empty($_GET['act']) ? trim($_GET['act']) : ''; switch($act) { case 'adda': $area['a_value'] = trim($_POST['a_value']); $area['a_type']=3; if(strpos($area['a_value'], "\n") === false) { //echo $area['a_value']; //add($area); //$D

JS代码同步文本框内容的实例方法_javascript技巧

HTML代码: 复制代码 代码如下: <html>    <head>      <script>       //同步函数       function synchronize(){         document.getElementById('i1').value =document.getElementById('i2').value;         //alert("同步成功");      }       //执行同步       s