textarea根据自适应增高。

在网上找了很久,都打算放弃的时候找到了。亲测可用。
http://ce.sysu.edu.cn/hope/Item/119193.aspx
-------------------------------------
<textarea onpropertychange = "this.style.height=this.scrollHeight+'px';" oninput = "this.style.height=this.scrollHeight+'px';" style="overflow-y:hidden;"></textarea>
----------------------------------------
 scrollHeight是文本区域的实际大小,height是这个文本框的大小。关键就是js侦测文本框的scrollHeight并将其赋值给height。现在这样写有时候会出现个问题,刚开始写的时候,文本框会越写越矮,这时就要给它添加最小高度。

===========================================
<textarea onpropertychange = "this.style.height=this.scrollHeight+'px';" oninput = "this.style.height=this.scrollHeight+'px';" style="overflow-y:hidden;min-height:100px;_height:100px"></textarea>
----------------------------------------
这里设了最小高度为100px,_height是给ie6的hack,能达到min-height的效果。

猜你喜欢

转载自kelvinqxlq.iteye.com/blog/2326280
今日推荐