<textarea> - HTML: HyperText Markup Language 编辑

The HTML <textarea> element represents a multi-line plain-text editing control, useful when you want to allow users to enter a sizeable amount of free-form text, for example a comment on a review or feedback form.

The above example demonstrates a number of features of <textarea>:

  • An id attribute to allow the <textarea> to be associated with a <label> element for accessibility purposes
  • A name attribute to set the name of the associated data point submitted to the server when the form is submitted.
  • rows and cols attributes to allow you to specify an exact size for the <textarea> to take. Setting these is a good idea for consistency, as browser defaults can differ.
  • Default content entered between the opening and closing tags. <textarea> does not support the value attribute.

The <textarea> element also accepts several attributes common to form <input>s, such as autocomplete, autofocus, disabled, placeholder, readonly, and required.

Attributes

This element includes the global attributes.

autocapitalize This API has not been standardized.
This is a non-standard attribute supported by WebKit on iOS (therefore nearly all browsers running on iOS, including Safari, Firefox, and Chrome), which controls whether and how the text value should be automatically capitalized as it is entered/edited by the user. The non-deprecated values are available in iOS 5 and later. Possible values are:
  • none: Completely disables automatic capitalization.
  • sentences: Automatically capitalize the first letter of sentences.
  • words: Automatically capitalize the first letter of words.
  • characters: Automatically capitalize all characters.
  • on: This deprecated API should no longer be used, but will probably still work. Deprecated since iOS 5.
  • off: This deprecated API should no longer be used, but will probably still work. Deprecated since iOS 5.
autocomplete
This attribute indicates whether the value of the control can be automatically completed by the browser. Possible values are:
  • off: The user must explicitly enter a value into this field for every use, or the document provides its own auto-completion method; the browser does not automatically complete the entry.
  • on: The browser can automatically complete the value based on values that the user has entered during previous uses.

If the autocomplete attribute is not specified on a <textarea> element, then the browser uses the autocomplete attribute value of the <textarea> element's form owner. The form owner is either the <form> element that this <textarea> element is a descendant of or the form element whose id is specified by the form attribute of the input element. For more information, see the autocomplete attribute in <form>.

autofocus
This Boolean attribute lets you specify that a form control should have input focus when the page loads. Only one form-associated element in a document can have this attribute specified.
cols
The visible width of the text control, in average character widths. If it is specified, it must be a positive integer. If it is not specified, the default value is 20.
disabled
This Boolean attribute indicates that the user cannot interact with the control. If this attribute is not specified, the control inherits its setting from the containing element, for example <fieldset>; if there is no containing element when the disabled attribute is set, the control is enabled.
form
The form element that the <textarea> element is associated with (its "form owner"). The value of the attribute must be the id of a form element in the same document. If this attribute is not specified, the <textarea> element must be a descendant of a form element. This attribute enables you to place <textarea> elements anywhere within a document, not just as descendants of form elements.
maxlength
The maximum number of characters (UTF-16 code units) that the user can enter. If this value isn't specified, the user can enter an unlimited number of characters.
minlength
The minimum number of characters (UTF-16 code units) required that the user should enter.
name
The name of the control.
placeholder
A hint to the user of what can be entered in the control. Carriage returns or line-feeds within the placeholder text must be treated as line breaks when rendering the hint. 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 <label> element tied to the input. See Labels and placeholders in <input>: The Input (Form Input) element for a full explanation.
readonly
This Boolean attribute indicates that the user cannot modify the value of the control. Unlike the disabled attribute, the readonly attribute does not prevent the user from clicking or selecting in the control. The value of a read-only control is still submitted with the form.
required
This attribute specifies that the user must fill in a value before submitting a form.
rows
The number of visible text lines for the control.
spellcheck
Specifies whether the <textarea> is subject to spell checking by the underlying browser/OS. The value can be:
  • true: Indicates that the element needs to have its spelling and grammar checked.
  • default : Indicates that the element is to act according to a default behavior, possibly based on the parent element's own spellcheck value.
  • false : Indicates that the element should not be spell checked.
wrap
Indicates how the control wraps text. Possible values are:
  • hard: The browser automatically inserts line breaks (CR+LF) so that each line has no more than the width of the control; the cols attribute must also be specified for this to take effect.
  • soft: The browser ensures that all line breaks in the value consist of a CR+LF pair, but does not insert any additional line breaks.
  • off This API has not been standardized.: Like soft but changes appearance to white-space: pre so line segments exceeding cols are not wrapped and the <textarea> becomes horizontally scrollable.

If this attribute is not specified, soft is its default value.

Styling with CSS

<textarea> is a replaced element — it has intrinsic dimensions, like a raster image. By default, its display value is block. Compared to other form elements it is relatively easy to style, with its box model, fonts, color scheme, etc. being easily manipulable using regular CSS.

Styling HTML forms provides some useful tips on styling <textarea>s.

Baseline inconsistency

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, on another browser it may be set on the bottom of the <textarea> box. Don't use vertical-align: baseline on it; the behavior is unpredictable.

Controlling whether a textarea is resizable

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 resize CSS property — resizing is enabled by default, but you can explicitly disable it using a resize value of none:

textarea {
  resize: none;
}

Styling valid and invalid values

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 :valid and :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;
}

Examples

Basic example

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>

Min and max length

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>

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.

Placeholder

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>

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 <label> element tied to the input. See Labels and placeholders in <input>: The Input (Form Input) element for a full explanation.

Disabled and readonly

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>

Technical summary

Content categoriesFlow content, phrasing content, Interactive content, listed, labelable, resettable, and submittable form-associated element.
Permitted contentText
Tag omissionNone, both the starting and ending tag are mandatory.
Permitted parentsAny element that accepts phrasing content.
Implicit ARIA roletextbox
Permitted ARIA rolesNo role permitted
DOM interfaceHTMLTextAreaElement

Specifications

SpecificationStatusComment
HTML Living Standard
The definition of '<textarea>' in that specification.
Living Standard
HTML5
The definition of '<textarea>' in that specification.
Recommendation
HTML 4.01 Specification
The definition of '<textarea>' in that specification.
Recommendation

Browser compatibility

BCD tables only load in the browser

See also

Other form-related elements:

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:155 次

字数:24298

最后编辑:6 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文