<textarea>

您正在阅读此内容的英文版本,因为该语系尚未翻译。 帮助我们翻译此文章吧!

HTML <textarea> 元素表示一个多行纯文本编辑控件。 

内存形式 Flow contentphrasing contentInteractive contentlistedlabelableresettable, and submittable form-associated element.
合法内容 Text
标签省略 不允许,开始标签和结束标签都不能省略。
合法父级 Any element that accepts phrasing content.
Permitted ARIA roles None
DOM 接口 HTMLTextAreaElement

属性

这个元素包含了全局属性

autocapitalize
iOS的非标准属性(iOS上的Safari。Firefox,Chrome都支持),文本是否自动首字母大写。在iOS5和之后的版本上有效。可能的值为:
  • none: 禁用首字母大写。
  • sentences: 句子的首字母大写。
  • words: 单词或者字母的首字母大写。
  • characters: 全部大写。
  • on:  自iOS 5废弃。
  • off: 自iOS 5废弃。
autocomplete HTML5
是否使用浏览器的记忆功能自动填充文本。可能的值有:
  • off: 不使用浏览器的记忆自动填充,使用者必须输入他们想要输入的所有内容。或者网页提供了自己的自动填充方法。
  • on: 浏览器根据用户之前输入的内容或者习惯,在用户输入的时候给出相应输入提示。

如果不指明autocomplete属性,浏览器会从父级的表单元素上解析是不是开启这个属性。表单元素可以是`textarea`元素的父级<form>或者`textarea`有跟表单相同的id(参见下面的form属性)。更多请查看<form>autocomplete 属性。

autofocus HTML5
页面加载完毕之后是否自动给本元素添加焦点。只有跟表格关联的才能使本属性生效。
cols
文本域的可视宽度。必须为正数,默认为20 (HTML5)。
disabled
禁用文本域。默认为false。如果未指定,也可以从父级上如<fieldset>继承而来。
form HTML5
指定跟自身相关联的表单。值必须为本文档内的表单的ID,如果未指定,就是跟当前所在的表单元素相关联。这就允许你在文档的任意地方放置文本域元素。
maxlength HTML5
允许用户输入的最大字符长度 (Unicode) 。未指定表示无限长度。
minlength HTML5
允许用户输入的最小字符长度(Unicode) 
name
元素的名称。
placeholder HTML5
向用户提示可以在控件中输入的内容。 在渲染提示时,占位符文本中的回车符(\r)或换行符(\n)一定会被作为行断(换行)处理。
readonly
不允许用户修改元素内文本。和 disabled 属性不同的是,这个能让用户点击和选择元素内的文本。如果在表单里,这个元素的值还是会跟随表单一起提交。
required HTML5
提示用户这个元素的内容必填。
rows
元素的输入文本的行数(显示的高度)。
selectionDirection HTML5
The direction in which selection occurred. This is "forward" if the selection was made from left-to-right in an LTR locale or right-to-left in an RTL locale, or "backward" if the selection was made in the opposite direction. This can be "none" if the selection direction is unknown.
selectionEnd
当前选中的最后一个字符的位置索引。
selectionStart
当前选中的第一个字符的位置索引。
spellcheck HTML5
该属性设为true时,表明该元素会做拼写和语法检查。属性值为default时,表明元素有默认行为,可能会基于父元素的spellcheck值。属性值为false时,表明元素不做拼写和语法检查。
wrap HTML5
指定文本换行的方式。默认为soft。可能的值为:
  • hard: 在文本到达元素最大宽度的时候,浏览器自动插入换行符(CR+LF) 。比如指定 cols值。
  • soft: 在到达元素最大宽度的时候,不会自动插入换行符。

DOM 接口

本元素是 HTMLTextAreaElement接口的实例。

Resizable textareas

Requires Gecko 2.0(Firefox 4 / Thunderbird 3.3 / SeaMonkey 2.1)

Gecko 2.0支持可变大小的文本域。是由 resize进行控制。默认开启,你可以用下面的css禁用此功能:

textarea {
  resize: none;
}

与CSS的交互

从CSS的角度来看<textarea>可被替换的元素 。HTML 规范没有指定 <textarea> 的基线。所以不同的浏览器可以在任何位置渲染文本域。在Gecko内核的浏览器中 <textarea> 的基线是文本的第一行的基线。其他的浏览器可能把元素的底部设置为基线。所以不要使用 vertical-align: baseline ,在不同浏览器中的外观是不确定的。

光栅图像(raster image)相似,文本域有默认的尺寸

示例

HTML Content

<textarea name="textarea" rows="10" cols="50">Write something here</textarea>

规范

Specification Status Comment
HTML Living Standard
<textarea>
Living Standard
HTML5
<textarea>
Recommendation
HTML 4.01 Specification
<textarea>
Recommendation

兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
textareaChrome Full support YesEdge Full support YesFirefox Full support Yes
Notes
Full support Yes
Notes
Notes Before Firefox 6, when a <textarea> was focused, the insertion point was placed at the end of the text by default. Other major browsers place the insertion point at the beginning of the text.
Notes A default background-image gradient is applied to all <textarea> elements, which can be disabled using background-image: none.
IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support Yes
Notes
Full support Yes
Notes
Notes Before Firefox 6, when a <textarea> was focused, the insertion point was placed at the end of the text by default. Other major browsers place the insertion point at the beginning of the text.
Notes A default background-image gradient is applied to all <textarea> elements, which can be disabled using background-image: none.
Opera Android Full support YesSafari iOS Full support Yes
Notes
Full support Yes
Notes
Notes Unlike other major browsers, a default style of opacity: 0.4 is applied to disabled <textarea> elements.
Samsung Internet Android Full support Yes
autocapitalize
Non-standard
Chrome No support NoEdge No support NoFirefox No support NoIE No support NoOpera No support NoSafari No support NoWebView Android No support NoChrome Android No support NoFirefox Android No support NoOpera Android No support NoSafari iOS No support NoSamsung Internet Android No support No
autocompleteChrome No support No
Notes
No support No
Notes
Notes See issue 758078.
Edge No support NoFirefox Full support 59IE No support NoOpera No support NoSafari Full support Yes
Notes
Full support Yes
Notes
Notes See bug 150731.
WebView Android No support NoChrome Android No support NoFirefox Android Full support 59Opera Android No support NoSafari iOS No support NoSamsung Internet Android No support No
autofocusChrome Full support YesEdge Full support YesFirefox Full support 4IE Full support 10Opera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 4Opera Android ? Safari iOS ? Samsung Internet Android Full support Yes
colsChrome Full support YesEdge Full support YesFirefox Full support YesIE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
disabledChrome Full support YesEdge Full support YesFirefox Full support YesIE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
formChrome Full support YesEdge Full support YesFirefox Full support YesIE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
maxlengthChrome Full support YesEdge Full support YesFirefox Full support 4IE Full support 10Opera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 4Opera Android ? Safari iOS ? Samsung Internet Android Full support Yes
minlengthChrome Full support YesEdge Full support YesFirefox Full support YesIE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
nameChrome Full support YesEdge Full support YesFirefox Full support YesIE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
placeholderChrome Full support YesEdge Full support YesFirefox Full support 4IE Full support 10Opera Full support 11.5Safari Full support 5WebView Android Full support YesChrome Android Full support YesFirefox Android Full support 4Opera Android Full support 11.5Safari iOS Full support 4Samsung Internet Android Full support Yes
readonlyChrome Full support YesEdge Full support YesFirefox Full support YesIE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
requiredChrome Full support YesEdge Full support YesFirefox Full support YesIE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
rowsChrome Full support YesEdge Full support YesFirefox Full support YesIE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
spellcheckChrome Full support YesEdge Full support YesFirefox Full support YesIE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
wrapChrome Full support YesEdge Full support YesFirefox Full support YesIE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown
Non-standard. Expect poor cross-browser support.
Non-standard. Expect poor cross-browser support.
See implementation notes.
See implementation notes.

Gecko notes

Starting in Gecko 6.0 (Firefox 6.0 / Thunderbird 6.0 / SeaMonkey 2.3), when a <textarea> is focused, the insertion point is placed at the beginning of the text by default, instead of at the end. This change makes Gecko's behavior consistent with other popular browsers.

Firefox for Android, by default, sets a background-image gradient on all <textarea> elements. This can be disabled using background-image: none.

参见