Textarea根据内容长度调整大小 [英] Textarea to resize based on content length

查看:232
本文介绍了Textarea根据内容长度调整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个textarea,我在框中键入我的文本,它根据需要增长,以避免必须处理滚动条,它需要在删除文本后收缩!
我不想沿着mootools或jquery路线走,因为我有一个轻量级的形式。

I need a textarea where I type my text in the box, it grows in length as needed to avoid having to deal with scroll bars and it need to shrink after delete text! I didn’t want to go down the mootools or jquery route because I have a lightweight form.

推荐答案

你可以通过设置为 1px 来检查内容的高度,然后阅读 scrollHeight 属性:

You can check the content's height by setting to 1px and then reading the scrollHeight property:

function textAreaAdjust(o) {
  o.style.height = "1px";
  o.style.height = (25+o.scrollHeight)+"px";
}

<textarea onkeyup="textAreaAdjust(this)" style="overflow:hidden"></textarea>

它适用于Firefox 3,IE 7,Safari,Opera和Chrome。

It works under Firefox 3, IE 7, Safari, Opera and Chrome.

这篇关于Textarea根据内容长度调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆