首页 > Web开发 > 详细

jQuery让textarea自适应高度

时间:2017-12-26 15:10:37      阅读:211      评论:0      收藏:0      [点我收藏+]
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>

<body>
<textarea name="" id="textarea" class="mytest"></textarea>
</body>
<script src="js/jquery-2.1.1.min.js"></script>
<script>
        //jQuery实现textarea高度根据内容自适应
        $.fn.extend({
          txtaAutoHeight: function () {
                return this.each(function () {
                var $this = $(this);
                      if (!$this.attr(initAttrH)) {
                         $this.attr(initAttrH, $this.outerHeight());
                    }
                    setAutoHeight(this).on(input, function () {
                        setAutoHeight(this);
                   });
                });
                 function setAutoHeight(elem) {
                    var $obj = $(elem);
                    return $obj.css({ height: $obj.attr(initAttrH), overflow-y: hidden }).height(elem.scrollHeight);
                 }
          }
         });
    
         //调用
         $(function () {
            $(".mytest").txtaAutoHeight();
         });
</script>
</html>

 

jQuery让textarea自适应高度

原文:https://www.cnblogs.com/liumangdashi/p/8118000.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!