<!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>
原文地址:https://www.cnblogs.com/liumangdashi/p/8118000.html
时间: 2024-10-09 03:42:03