动态增加文本框高度?
可能的重复:
自动调整文本区域大小
大家好,我正在尝试解决一个问题,但毫无进展。我想做的是当用户文本溢出输入框的宽度时动态更改输入框的高度(有点像 Facebook 的状态更新文本框)。
您输入简短的更新,如果文本比文本框长,则会创建一个新行。我尝试使用文本区域,但无论出于何种原因,默认情况下都不能强制它恰好为 1 行。
有人知道这样做的简单方法吗? :(
Possible Duplicate:
Autosizing Textarea
Hello all, I am trying to solve a problem and getting absolutely no where with it. What I am trying to do is dynamically change the height of an inputbox when the users text overflows it's width (sorta like Facebook's status update textbox).
You enter a brief update, and if the text is longer than the textbox, it creates a new row. I tried using a textarea, but for whatever reason cannot force it to be exactly 1 row by default.
Anyone know an easy way of doing this? :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 CSS 高度和宽度参数手动控制
textarea
的大小。通过将按键事件绑定到textarea
,您可以获取框中的文本量并相应地调整高度。例如,这里有一些 jQuery,每输入 50 个字符,框的高度就会增加 20 个像素:您可以调整这些值以获得所需的效果。
You can manually control the size of the
textarea
using the CSS height and width parameters. By binding a keypress event to thetextarea
, you can get the amount of text in the box and adjust the height accordingly. For example, here's some jQuery that will add 20 pixels to the height of the box for every 50 characters you put in:You can tweak the values to get the desired effect.
要拉伸
textarea
,请尝试一下 (jQuery): http: //www.unwrongest.com/projects/elastic/。至于恰好一行的高度,您可以尝试
另一种解决方案是在屏幕上使用
div
,当用户单击它时,使浏览器聚焦在隐藏的文本框上。用户在隐藏的文本框中键入内容,然后div
就会通过 获得文本框的内容。 JavaScript。棘手的一点是获得闪烁的光标,但是您可以在 div 中使用管道|
字符并使其闪烁,尽管这在您启动它时开始变得冗长。我个人会再次尝试使用文本区域:-)希望这有帮助,
詹姆斯
To get the
textarea
to stretch, give this a go (jQuery): http://www.unwrongest.com/projects/elastic/.As for the height of exactly one row, you could try
rows="1"
in the<textarea>
tag, as well as removing CSS styling. Some browsers, however, may set a textarea's minimum height to more than one row.Another solution would be to use a
div
on screen that, when the user clicks on it, makes the browser focus on a hidden textbox. The user types into the hidden text box, and thediv
is given the textbox's contents via. JavaScript. The tricky bit then is getting the flashing cursor, but you could use the pipe|
character in the div and make it flash, although this starts getting long winded about the time you start it. I'd try with the textarea again personally :-)Hope this helps,
James