我创建了一个带有输入的表单,但该框只能处理单行中的文本。我想设计它的样式,使输入字段类似于 Twitter 的输入字段,其中框本身是多行:
当您按下回车键时它也会展开:
这是我目前拥有的:
<form name="userForm">
<input type="text" id="userInput" name="userInput" placeholder="Enter text here.">
<button type="submit" id="button">Submit</button>
</form>
我已经设置了按钮和输入框的样式,但没有做任何改变其形状的事情,所以它是默认的。我需要调整什么才能实现这一目标?
最佳答案
您需要使用 HTML <textarea>
元素。
来自 MDN:
The HTML
<textarea>
element represents a multi-line plain-text editing control.
使用 <input>
对于多行文本, native 是不可能的,如果被黑客攻击,将是无效的 HTML。
HTML5 规范:
4.10.5.1.2 Text (
type=text
) state and Search state (type=search
)The
input
element represents a one line plain text edit control for the element's value.(emphasis mine)
推特输入框
您提到您希望文本区域类似于 Twitter(自动调整大小/无滚动条)。考虑这个选项和以下 SO 帖子:
A small, stand-alone script to automatically adjust textarea height.
关于html - 使输入元素(类型 ="text")处理多行文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35542278/