textarea高度自適應

來源:互聯網
上載者:User

3個知識點:

1.事件觸發,onpropertychange for ie,oninput for ff (http://www.cnblogs.com/bravfing/archive/2012/04/10/2440681.html)。

2.通過scrollHeight得到內容實際高度然後賦值給height。

3.過程2裡有個問題,for ff的,是因為ff對scrollHeight的解釋是大於等於clientHeight的,所以當文本行數變少的時候,scollHeight不能得到真實值,這裡有個小技巧,就是先賦給height一個最小值就可以了。

最後,如果textarea是百分比寬度的,當window resize的時候,要主動觸發賦值事件。代碼如下:

var auto = function(el){var min = el.height(),ev_name;// for ieif(ie){ev_name = "propertychange";}// for otherelse{ev_name = "input";}el.on(ev_name,function(ev){if(!ie)el.height(min);el.height(Math.max(this.scrollHeight,min));});// window resize時候主動觸發$(window).on("resize",function(){el.trigger(ev_name);});};auto($("#to"));

  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.