--- title: slug: Web/HTML/Element/textarea tags: - HTML - HTML表单 - textarea - 元素 - 参考 - 文本区域 - 表单 translation_of: Web/HTML/Element/textarea --- HTML <textarea> 元素表示一个多行纯文本编辑控件,当你希望用户输入一段相当长的、不限格式的文本,例如评论或反馈表单中的一段意见时,这很有用。 {{EmbedInteractiveExample("pages/tabbed/textarea.html", "tabbed-standard")}} 上述例子展示了 <textarea> 的几个特点: 为了提高可访问性(accessibility),用于将 <textarea>与一个 {{htmlelement("label")}} 关联的 id 属性。 name 属性,用于设置随表单一同提交到服务器的相关数据的名字。 rows 和 cols 属性,用于声明 <textarea> 的精确尺寸。这对于一致性非常有帮助,因为不同浏览器的默认值常常不一样。 位于开始标签和闭合标签之间的默认内容。<textarea> 不支持 value 属性。 <textarea> 还可以使用 <input> 中的一些常见属性,如autocomplete, autofocus, disabled, placeholder, readonly, 和 required。 属性 这个元素包含了全局属性。 {{htmlattrdef("autocapitalize")}} {{non-standard_inline}} iOS的非标准属性(运行在iOS上的Safari、Firefox、Chrome都支持),文本是否自动首字母大写。在iOS5和之后的版本上有效。可能的值为: none: 禁用首字母大写。 sentences: 句子的首字母大写。 words: 单词或者字母的首字母大写。 characters: 全部字母大写。 on: {{deprecated_inline()}} 自iOS 5废弃。 off: {{deprecated_inline()}} 自iOS 5废弃。 {{htmlattrdef("autocomplete")}} 是否使用浏览器的记忆功能自动填充文本。可能的值有: off: 不使用浏览器的记忆自动填充,使用者必须输入他们想要输入的所有内容。或者网页提供了自己的自动填充方法。 on: 浏览器根据用户之前输入的内容或者习惯,在用户输入的时候给出相应输入提示。 如果不指明autocomplete属性,浏览器会从父级的表单元素上解析是不是开启这个属性。表单元素可以是`textarea`元素的父级{{HTMLElement("form")}}或者`textarea`有跟表单相同的id(参见下面的form属性)。更多请查看{{HTMLElement("form")}}的{{htmlattrxref("autocomplete", "form")}} 属性。 {{ htmlattrdef("autofocus") }} 页面加载完毕之后是否自动给本元素添加焦点。只有跟表格关联的才能使本属性生效。 {{ htmlattrdef("cols") }} 文本域的可视宽度。必须为正数,默认为20 (HTML5)。 {{ htmlattrdef("disabled") }} 禁用文本域。默认为false。如果未指定,也可以从父级上如{{ HTMLElement("fieldset") }}继承而来。 {{ htmlattrdef("form") }} 指定跟自身相关联的表单。值必须为本文档内的表单的ID,如果未指定,就是跟当前所在的表单元素相关联。这就允许你在文档的任意地方放置文本域元素。 {{ htmlattrdef("maxlength") }} 允许用户输入的最大字符长度 (Unicode) 。未指定表示无限长度。 {{ htmlattrdef("minlength") }} 允许用户输入的最小字符长度(Unicode) {{ htmlattrdef("name") }} 元素的名称。 {{ htmlattrdef("placeholder") }} 向用户提示可以在控件中输入的内容。 在渲染提示时,占位符文本中的回车符(\r)或换行符(\n)一定会被作为行断(换行)处理。 Note: Placeholders should only be used to show an example of the type of data that should be entered into a form; they are not a substitute for a proper {{HTMLElement("label")}} element tied to the input. See {{SectionOnPage("/en-US/docs/Web/HTML/Element/input", "Labels and placeholders")}} for a full explanation. {{ htmlattrdef("readonly") }} 不允许用户修改元素内文本。和 disabled 属性不同的是,这个能让用户点击和选择元素内的文本。如果在表单里,这个元素的值还是会跟随表单一起提交。 {{ htmlattrdef("required") }} 提示用户这个元素的内容必填。 {{ htmlattrdef("rows") }} 元素的输入文本的行数(显示的高度)。 {{ htmlattrdef("spellcheck") }} 该属性设为true时,表明该元素会做拼写和语法检查。属性值为default时,表明元素有默认行为,可能会基于父元素的spellcheck值。属性值为false时,表明元素不做拼写和语法检查。 {{ htmlattrdef("wrap") }} 指定文本换行的方式。默认为soft。可能的值为: hard: 在文本到达元素最大宽度的时候,浏览器自动插入换行符(CR+LF) 。比如指定 cols值。 soft: 在到达元素最大宽度的时候,不会自动插入换行符。 使用 CSS 样式 <textarea> 是 可被替换的元素——其有自己的固有尺寸,像。默认情况下,其 {{cssxref("display")}} 值为 block。与其他表单元素相比,这个元素相对比较容易设置样式,使用常规的 CSS 就可以轻松设置它的盒模型、字体、颜色方案等。 Styling HTML forms 这个页面提供了一些关于 <textarea> 样式的有用提示。 基线不一致 The HTML specification doesn't define where the baseline of a <textarea> is, so different browsers set it to different positions. For Gecko, the <textarea> baseline is set on the baseline of the first line of the textarea's first line, on another browser it may be set on the bottom of the <textarea> box. Don't use {{cssxref("vertical-align")}}: baseline on it; the behavior is unpredictable. 控制文本区是否可调整大小 In most browsers, <textarea>s are resizable — you'll notice the drag handle in the right hand corner, which can be used to alter the size of the element on the page. This is controlled by the {{ cssxref("resize") }} CSS property — resizing is enabled by default, but you can explicitly disable it using a resize value of none: textarea { resize: none; } 在样式中使用有效值和无效值 Valid and invalid values of a <textarea> element (e.g. those within, and outside the bounds set by minlength, maxlength, or required) can be highlighted using the {{cssxref(":valid")}} and {{cssxref(":invalid")}} pseudo-classes. For example, to give your textarea a different border depending on whether it is valid or invalid: textarea:invalid { border: 2px dashed red; } textarea:valid { border: 2px solid lime; } 示例 HTML 内容 <textarea name="textarea" rows="10" cols="50">Write something here</textarea> {{ EmbedLiveSample('Example','600','200') }} 基本示例 The following example show a very simple textarea, with a set numbers of rows and columns and some default content. <textarea name="textarea" rows="10" cols="50">Write something here</textarea> {{ EmbedLiveSample('Basic_example','600','150') }} 最小和最大长度 This example has a minimum and maximum number of characters — of 10 and 20 respectively. Try it and see. <textarea name="textarea" rows="5" cols="30" minlength="10" maxlength="20">Write something here</textarea> {{ EmbedLiveSample('Min_and_max_length','600','80') }} Note that minlength doesn't stop the user from removing characters so that the number entered goes past the minimum, but it does make the value entered into the <textarea> invalid. Also note that even if you have a minlength value set (3, for example), an empty <textarea> is still considered valid unless you also have the required attribute set. 占位符 This example has a placeholder set. Notice how it disappears when you start typing into the box. <textarea name="textarea" rows="5" cols="30" placeholder="Comment text."></textarea> {{ EmbedLiveSample('Placeholder','600','80') }} Note: Placeholders should only be used to show an example of the type of data that should be entered into a form; they are not a substitute for a proper {{HTMLElement("label")}} element tied to the input. See {{SectionOnPage("/en-US/docs/Web/HTML/Element/input", "Labels and placeholders")}} for a full explanation. 禁用和只读 This example shows two <textarea>s — one of which is disabled, and one of which is readonly. Have a play with both and you'll see the difference in behavior — the disabled element is not selectable in any way (and its value is not submitted), whereas the readonly element is selectable and its contents copyable (and its value is submitted); you just can't edit the contents. Note: In browsers other than firefox, such as chrome, the disabled textarea content may be selectable and copyable. <textarea name="textarea" rows="5" cols="30" disabled>I am a disabled textarea</textarea> <textarea name="textarea" rows="5" cols="30" readonly>I am a readonly textarea</textarea> {{ EmbedLiveSample('Disabled_and_readonly','600','80') }} 技术概览 Content categories Flow content, phrasing content, Interactive content, listed, labelable, resettable, and submittable form-associated element. Permitted content Text Tag omission {{no_tag_omission}} Permitted parents Any element that accepts phrasing content. Implicit ARIA role textbox Permitted ARIA roles No role permitted DOM interface {{domxref("HTMLTextAreaElement")}} 规范 Specification Status Comment {{SpecName('HTML WHATWG', 'forms.html#the-textarea-element', '<textarea>')}} {{Spec2('HTML WHATWG')}} {{SpecName('HTML5 W3C', 'sec-forms.html#the-textarea-element', '<textarea>')}} {{Spec2('HTML5 W3C')}} {{SpecName('HTML4.01', 'interact/forms.html#h-17.7', '<textarea>')}} {{Spec2('HTML4.01')}} 浏览器兼容性 {{Compat("html.elements.textarea")}} 参见 其他与表单相关的元素: {{ HTMLElement("form") }} {{ HTMLElement("button") }} {{ HTMLElement("datalist") }} {{ HTMLElement("legend") }} {{ HTMLElement("label") }} {{ HTMLElement("select") }} {{ HTMLElement("optgroup") }} {{ HTMLElement("option") }} {{ HTMLElement("input") }} {{ HTMLElement("keygen") }} {{ HTMLElement("fieldset") }} {{ HTMLElement("output") }} {{ HTMLElement("progress") }} {{ HTMLElement("meter") }}
HTML <textarea> 元素表示一个多行纯文本编辑控件,当你希望用户输入一段相当长的、不限格式的文本,例如评论或反馈表单中的一段意见时,这很有用。
<textarea>
{{EmbedInteractiveExample("pages/tabbed/textarea.html", "tabbed-standard")}}
上述例子展示了 <textarea> 的几个特点:
id
name
rows
cols
value
<textarea> 还可以使用 <input> 中的一些常见属性,如autocomplete, autofocus, disabled, placeholder, readonly, 和 required。
<input>
autocomplete
autofocus
disabled
placeholder
readonly
required
这个元素包含了全局属性。
none
sentences
words
characters
on
off
如果不指明autocomplete属性,浏览器会从父级的表单元素上解析是不是开启这个属性。表单元素可以是`textarea`元素的父级{{HTMLElement("form")}}或者`textarea`有跟表单相同的id(参见下面的form属性)。更多请查看{{HTMLElement("form")}}的{{htmlattrxref("autocomplete", "form")}} 属性。
Note: Placeholders should only be used to show an example of the type of data that should be entered into a form; they are not a substitute for a proper {{HTMLElement("label")}} element tied to the input. See {{SectionOnPage("/en-US/docs/Web/HTML/Element/input", "Labels and placeholders")}} for a full explanation.
<textarea> 是 可被替换的元素——其有自己的固有尺寸,像。默认情况下,其 {{cssxref("display")}} 值为 block。与其他表单元素相比,这个元素相对比较容易设置样式,使用常规的 CSS 就可以轻松设置它的盒模型、字体、颜色方案等。
block
Styling HTML forms 这个页面提供了一些关于 <textarea> 样式的有用提示。
The HTML specification doesn't define where the baseline of a <textarea> is, so different browsers set it to different positions. For Gecko, the <textarea> baseline is set on the baseline of the first line of the textarea's first line, on another browser it may be set on the bottom of the <textarea> box. Don't use {{cssxref("vertical-align")}}: baseline on it; the behavior is unpredictable.
: baseline
In most browsers, <textarea>s are resizable — you'll notice the drag handle in the right hand corner, which can be used to alter the size of the element on the page. This is controlled by the {{ cssxref("resize") }} CSS property — resizing is enabled by default, but you can explicitly disable it using a resize value of none:
resize
textarea { resize: none; }
Valid and invalid values of a <textarea> element (e.g. those within, and outside the bounds set by minlength, maxlength, or required) can be highlighted using the {{cssxref(":valid")}} and {{cssxref(":invalid")}} pseudo-classes. For example, to give your textarea a different border depending on whether it is valid or invalid:
minlength
maxlength
textarea:invalid { border: 2px dashed red; } textarea:valid { border: 2px solid lime; }
<textarea name="textarea" rows="10" cols="50">Write something here</textarea>
{{ EmbedLiveSample('Example','600','200') }}
The following example show a very simple textarea, with a set numbers of rows and columns and some default content.
{{ EmbedLiveSample('Basic_example','600','150') }}
This example has a minimum and maximum number of characters — of 10 and 20 respectively. Try it and see.
<textarea name="textarea" rows="5" cols="30" minlength="10" maxlength="20">Write something here</textarea>
{{ EmbedLiveSample('Min_and_max_length','600','80') }}
Note that minlength doesn't stop the user from removing characters so that the number entered goes past the minimum, but it does make the value entered into the <textarea> invalid. Also note that even if you have a minlength value set (3, for example), an empty <textarea> is still considered valid unless you also have the required attribute set.
This example has a placeholder set. Notice how it disappears when you start typing into the box.
<textarea name="textarea" rows="5" cols="30" placeholder="Comment text."></textarea>
{{ EmbedLiveSample('Placeholder','600','80') }}
This example shows two <textarea>s — one of which is disabled, and one of which is readonly. Have a play with both and you'll see the difference in behavior — the disabled element is not selectable in any way (and its value is not submitted), whereas the readonly element is selectable and its contents copyable (and its value is submitted); you just can't edit the contents.
Note: In browsers other than firefox, such as chrome, the disabled textarea content may be selectable and copyable.
<textarea name="textarea" rows="5" cols="30" disabled>I am a disabled textarea</textarea> <textarea name="textarea" rows="5" cols="30" readonly>I am a readonly textarea</textarea>
{{ EmbedLiveSample('Disabled_and_readonly','600','80') }}
textbox
role
{{Compat("html.elements.textarea")}}
其他与表单相关的元素: