jQuery實現textarea高度根據內容自適應


 1         //jQuery實現textarea高度根據內容自適應
 2         $.fn.extend({
 3             txtaAutoHeight: function () {
 4                 return this.each(function () {
 5                     var $this = $(this);
 6                     if (!$this.attr('initAttrH')) {
 7                         $this.attr('initAttrH', $this.outerHeight());
 8                     }
 9                     setAutoHeight(this).on('input', function () {
10                         setAutoHeight(this);
11                     });
12                 });
13                 function setAutoHeight(elem) {
14                     var $obj = $(elem);
15                     return $obj.css({ height: $obj.attr('initAttrH'), 'overflow-y': 'hidden' }).height(elem.scrollHeight);
16                 }
17             }
18         });
19 
20         //調用
21         $(function () {
22             $("#txtaMain").txtaAutoHeight();
23         });

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM