textarea高度自适应
我们见到的文本框textarea他的宽度和高度大都是一成不变的,就是在设计时他的宽度和高度是多少,在浏览者看到的就是那个宽度和高度。那么我们有没有办法实现textarea的高度随着用户的内容的改变而改变呢?就是用户输入一行他的高度就是一行,输入两行,他的高度就是两行。下面给出两种实现方法:
一、利用JS加CSS自动调整
<textarea name="mytextarea" cols="80" style="overflow-y:hidden;height:80px;" onpropertychange="this.style.height=this.scrollHeight+'px';" oninput="this.style.height=this.scrollHeight+'px';"></textarea>
提示:点击运行代码按钮可看到实际效果
二、利用CSS控制
<textarea style="width:300px;overflow-y:visible"></textarea>
提示:点击运行代码按钮可看到实际效果
这种方法比较简单,推荐使用。
更新时间:2010-5-29
|