HTMLInputElement - Web API 接口参考 编辑

HTMLInputElement 接口提供了特定的属性和方法(继承自常规的HTML元素接口)用于管理输入元素的布局和外观。

  <div id="interfaceDiagram" style="display: inline-block; position: relative; width: 100%; padding-bottom: 20%; vertical-align: middle; overflow: hidden;"><svg style="display: inline-block; position: absolute; top: 0; left: 0;" viewbox="-50 0 600 120" preserveAspectRatio="xMinYMin meet"><a xlink:href="/wiki/zh-CN/docs/Web/API/EventTarget" target="_top"><rect x="1" y="1" width="110" height="50" fill="#fff" stroke="#D4DDE4" stroke-width="2px" /><text  x="56" y="30" font-size="12px" font-family="Consolas,Monaco,Andale Mono,monospace" fill="#4D4E53" text-anchor="middle" alignment-baseline="middle">EventTarget</text></a><polyline points="111,25  121,20  121,30  111,25" stroke="#D4DDE4" fill="none"/><line x1="121" y1="25" x2="151" y2="25" stroke="#D4DDE4"/><a xlink:href="/wiki/zh-CN/docs/Web/API/Node" target="_top"><rect x="151" y="1" width="75" height="50" fill="#fff" stroke="#D4DDE4" stroke-width="2px" /><text  x="188.5" y="30" font-size="12px" font-family="Consolas,Monaco,Andale Mono,monospace" fill="#4D4E53" text-anchor="middle" alignment-baseline="middle">Node</text></a><polyline points="226,25  236,20  236,30  226,25" stroke="#D4DDE4" fill="none"/><line x1="236" y1="25" x2="266" y2="25" stroke="#D4DDE4"/><a xlink:href="/wiki/zh-CN/docs/Web/API/Element" target="_top"><rect x="266" y="1" width="75" height="50" fill="#fff" stroke="#D4DDE4" stroke-width="2px" /><text  x="303.5" y="30" font-size="12px" font-family="Consolas,Monaco,Andale Mono,monospace" fill="#4D4E53" text-anchor="middle" alignment-baseline="middle">Element</text></a><polyline points="341,25  351,20  351,30  341,25" stroke="#D4DDE4" fill="none"/><line x1="351" y1="25" x2="381" y2="25" stroke="#D4DDE4"/><a xlink:href="https://developer.mozilla.org/wiki/zh-CN/docs/Web/API/HTMLElement" target="_top"><rect x="381" y="1" width="110" height="50" fill="#fff" stroke="#D4DDE4" stroke-width="2px" /><text  x="436" y="30" font-size="12px" font-family="Consolas,Monaco,Andale Mono,monospace" fill="#4D4E53" text-anchor="middle" alignment-baseline="middle">HTMLElement</text></a><polyline points="491,25  501,20  501,30  491,25" stroke="#D4DDE4" fill="none"/><line x1="501" y1="25" x2="509" y2="25" stroke="#D4DDE4"/><line x1="509" y1="25" x2="509" y2="90" stroke="#D4DDE4"/><line x1="509" y1="90" x2="492" y2="90" stroke="#D4DDE4"/><a xlink:href="/wiki/zh-CN/docs/Web/API/HTMLInputElement" target="_top"><rect x="331" y="65" width="160" height="50" fill="#F4F7F8" stroke="#D4DDE4" stroke-width="2px" /><text  x="411" y="94" font-size="12px" font-family="Consolas,Monaco,Andale Mono,monospace" fill="#4D4E53" text-anchor="middle" alignment-baseline="middle">HTMLInputElement</text></a></svg></div>
  a:hover text { fill: #0095DD; pointer-events: all;}

属性

与父表单相关的属性
form只读HTMLFormElement object:  返回一个父表单元素的引用。
formActionstring: 返回/ 设置 元素的 formaction 属性, 包含处理元素提交信息程序的URI. 这会重写父表单的 action 属性.
formEncTypestring: 返回/ 设置 元素的 formenctype 属性, 包含将表单提交到服务器的内容类型. 这会重写父表单的 enctype 属性.
formMethodstring: 返回/ 设置 元素的 formmethod 属性, 包含浏览器用于提交表单的HTTP方法. 这会重写父表单的 method 属性.
formNoValidateboolean: 返回/ 设置 元素的 formnovalidate 属性, 表示在表单提交时不对其进行验证. 这会重写父表单的 novalidate 属性.
formTargetstring: 返回/ 设置 元素的 formtarget 属性, 包含一个名称或关键字, 表示在提交表单后接收响应的显示位置. 这会重写父表单的 target 属性.
可用于任何非隐藏输入元素的属性
namestring: 返回/ 设置 元素的 name 属性, 包含名称.
typestring: 返回/ 设置 元素的 type 属性, 包含显示类型. 查看 <input>type 属性可用值.
disabledboolean: 返回/ 设置 元素的 disabled 属性, 表示是否禁用 <input>. 该元素的值将不会被提交. 也可以查看 readonly 
autofocusboolean: 返回/ 设置 元素的 autofocus 属性, 指定的 <input> 在页面加载时应当具有输入焦点, 例如通过键入不同的控件. 在文档中只有一个表单元素可以拥有 autofocus 属性. 如果 type 属性被设置为 hidden 则无效(即不可为隐藏控件设置自动焦点).
requiredboolean: 返回/ 设置 元素的 required 属性, 表示用户必填项.
valuestring: 返回/ 设置 当前控件的值.

提示: 如果用户输入与预期不同,可能会返回空.

validity 只读ValidityState object: Returns the validity state that this element is in.
validationMessage 只读string: Returns a localized message that describes the validation constraints that the control does not satisfy (if any). This is the empty string if the control is not a candidate for constraint validation (willvalidate is false), or it satisfies its constraints.
willValidate只读Boolean: Indicates whether the element is a candidate for constraint validation. It is false if any conditions bar it from constraint validation.
仅适用于类型为"checkbox" 或 "radio"元素的属性
checked boolean: 返回/ 设置 当前选中状态,当控件type 是 checkbox 或 radio时.
defaultCheckedboolean: 返回/ 设置 the default state of a radio button or checkbox as originally specified in HTML that created this object.
indeterminateboolean: indicates that the checkbox is neither on nor off. Changes the appearance to resemble a third state. Does not affect the value of the checked 属性, and clicking the checkbox will set the value to false.
仅适用于类型为"image"元素的属性
altstring: 返回/ 设置 元素的 alt 属性, 包含 alternative text to use when type is image.
heightstring: 返回/ 设置 元素的 height 属性, which defines the height of the image displayed for the button, if the value of type is image.
srcstring: 返回/ 设置 元素的 src 属性, which specifies a URI for the location of an image to display on the graphical submit button, if the value of type is image; otherwise it is ignored.
widthstring: 返回/ 设置 the document's width 属性, which defines the width of the image displayed for the button, if the value of type is image.
仅适用于类型为"file"元素的属性
acceptstring: Returns / Sets 元素的 accept 属性, 包含 comma-separated list of file types accepted by the server when type is file.
allowdirs boolean: Part of the non-standard Directory Upload API; indicates whether or not to allow directories and files both to be selected in the file list. Implemented only in Firefox and is hidden behind a preference.
filesReturns/accepts a FileList object, which contains a list of File objects representing the files selected for upload.
webkitdirectory boolean: Returns the webkitdirectory 属性; if true, the file system picker interface only accepts directories instead of files.
webkitEntries Array of FileSystemEntry objects describing the currently-selected files or directories.
仅适用于文本或数字输入元素的属性
autocompletestring: 返回/ 设置 元素的 autocomplete 属性, indicating whether the value of the control can be automatically completed by the browser. Ignored if the value of the type 属性is hidden, checkbox, radio, file, or a button type (button, submit, reset, image). Possible values are:
"on": the browser can autocomplete the value using previously stored value
"off": the user must explicity enter a value
maxLengthlong: 返回/ 设置 元素的 maxlength 属性, 包含 the maximum length of characters (in Unicode code points) that the value can have. (If you set this to a negative number, an exception will be thrown.)
sizeunsigned long: 返回/ 设置 元素的 size 属性, 包含 size of the control. This value is in pixels unless the value of type is text or password, in which case, it is an integer number of characters. Applies only when type is set to text, search, tel, url, email, or password; otherwise it is ignored.
patternstring: 返回/ 设置 元素的 pattern 属性, 包含 a regular expression that the control's value is checked against.  Use the title 属性to describe the pattern to help the user. This 属性applies when the value of the type 属性is text, search, tel, url or email; otherwise it is ignored.
placeholderstring: 返回/ 设置 元素的 placeholder 属性, 包含 a hint to the user of what can be entered in the control. The placeholder text must not contain carriage returns or line-feeds. This 属性applies when the value of the type 属性is text, search, tel, url or email; otherwise it is ignored.
readOnlyboolean: 返回/ 设置 元素的 readonly 属性, indicating that the user cannot modify the value of the control.
HTML5This is ignored if the value of the type 属性is hidden, range, color, checkbox, radio, file, or a button type.
minstring: 返回/ 设置 元素的 min 属性, 包含 the minimum (numeric or date-time) value for this item, which must not be greater than its maximum (max 属性) value.
maxstring: 返回/ 设置 元素的 max 属性, 包含 the maximum (numeric or date-time) value for this item, which must not be less than its minimum (min 属性) value.
selectionStartunsigned long: 返回/ 设置 the beginning index of the selected text. When nothing is selected, this returns the position of the text input cursor (caret) inside of the <input> element.
selectionEndunsigned long: 返回/ 设置 the end index of the selected text. When there's no selection, this returns the offset of the character immediately following the current text input cursor position.
selectionDirectionstring: 返回/ 设置 the direction in which selection occurred. Possible values are:
"forward" if selection was performed in the start-to-end direction of the current locale,
"backward" for the opposite direction,
"none" if the direction is unknown."
未分类的属性
defaultValuestring: 返回/ 设置 the default value as originally specified in the HTML that created this object.
dirNamestring: 返回/ 设置 the directionality of the element.
accessKeystring: 返回 a string 包含 a single character that switches input focus to the control when pressed.
list 只读HTMLElement object: 返回 the element pointed by the list 属性. The property may be null if no HTML element found in the same tree.
multipleboolean: 返回/ 设置 元素的 multiple 属性, indicating whether more than one value is possible (e.g., multiple files).
filesFileList array: 返回the list of selected files.
HTMLInputElement.labels 只读NodeList array: 返回a list of <label> elements that are labels for this element.
stepstring: 返回/ 设置 元素的 step 属性, which works with min and max to limit the increments at which a numeric or date-time value can be set. It can be the string any or a positive floating point number. If this is not set to any, the control accepts only values at multiples of the step value greater than the minimum.
valueAsDateDate object: Returns / Sets the value of the element, interpreted as a date, or null if conversion is not possible.
valueAsNumberdouble: 返回the value of the element, interpreted as one of the following, in order:
  • a time value
  • a number
  • NaN if conversion is impossible
autocapitalize string: 定义 the capitalization behavior for user input. Valid values are none, off, characters, words, or sentences.
HTMLInputElement.align
string: represents the alignment of the element. Use CSS instead.
HTMLInputElement.useMap
string: represents a client-side image map.

方法

focus()Focus on the input element; keystrokes will subsequently go to this element.
blur()Removes focus from input; keystrokes will subsequently go nowhere.
select()Selects the input text in the element, and focuses it so the user can subsequently replace the whole entry.
click()Simulates a click on the element.
setSelectionRange()Selects a range of text in the element (but does not focus it). The optional selectionDirection parameter may be "forward" or "backward" to establish the direction in which selection was set, or "none" if the direction is unknown or not relevant. The default is "none". Specifying a selectionDirection parameter sets the value of the selectionDirection property.
setRangeText()

Replaces a range of text with the new text. If the start and end arguments are not provided, the range is assumed to be the selection.The final argument determines how the selection should be set after the text has been replaced. The possible values are:

"select"
Selects the newly inserted text.
"start"
Moves the selection to just before the inserted text.
"end"
Moves the selection to just after the selected text.
"preserve"
Attempts to preserve the selection. This is the default.
setCustomValidity()Sets a custom validity message for the element. If this message is not the empty string, then the element is suffering from a custom validity error, and does not validate.
checkValidity()Returns a Boolean that is false if the element is a candidate for constraint validation, and it does not satisfy its constraints. In this case, it also fires an invalid event at the element. It returns true if the element is not a candidate for constraint validation, or if it satisfies its constraints.
HTMLInputElement.stepDown()
Decrements the value by (step * n), where n defaults to 1 if not specified. Throws an INVALID_STATE_ERR exception:
  • if the method is not applicable to for the current type value,
  • if the element has no step value,
  • if the value cannot be converted to a number,
  • if the resulting value is above the max or below the min
HTMLInputElement.stepUp()
Increments the value by (step * n), where n defaults to 1 if not specified. Throws an INVALID_STATE_ERR exception:
  • if the method is not applicable to for the current type value.,
  • if the element has no step value,
  • if the value cannot be converted to a number,
  • if the resulting value is above the max or below the min.
HTMLInputElement.mozSetFileArray()
Sets the files selected on the input to the given array of File objects.  This is an alternative to mozSetFileNameArray() which can be used in frame scripts: a chrome script can open files as File objects and send them via message manager.
HTMLInputElement.mozGetFileNameArray()
Returns an array of all the file names from the input.
HTMLInputElement.mozSetFileNameArray()
Sets the filenames for the files selected on the input.  Not for use in frame scripts, because it accesses the file system.

    规范

    SpecificationStatusComment
    HTML Living Standard
    HTMLInputElement
    Living StandardNo change from HTML5
    HTML5
    HTMLInputElement
    RecommendationTechnically, the  tabindex and accesskey properties, as well as the blur(), click(), and focus() methods, are now defined on HTMLElement.
    The following properties are now obsolete: align and useMap.
    The following properties have been added: autocomplete, autofocus, dirName, files, formAction, formEncType, formMethod, formNoValidate, formTarget, height, indeterminate, labels, list, max, min, multiple, pattern, placeholder, required, selectionDirection, selectionEnd, selectionStart, step, validationMessage, validity, valueAsDate, valueAsNumber, width, and willValidate.
    The following methods have been added: checkValidity(), setCustomValidity(), setSelectionRange(), stepUp(), and stepDown().
    Document Object Model (DOM) Level 2 HTML Specification
    HTMLInputElement
    ObsoleteThe size property is now an unsigned long. The type property must be entirely given in lowercase characters.
    Document Object Model (DOM) Level 1 Specification
    HTMLInputElement
    ObsoleteInitial definition.

    浏览器兼容性

    We're converting our compatibility data into a machine-readable JSON format. This compatibility table still uses the old format, because we haven't yet converted the data it contains. Find out how you can help!
    FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
    Basic support(Yes)(Yes)1.0 (1.7 or earlier)(Yes)(Yes)(Yes)
    autocomplete & autofocus properties(Yes)(Yes)(Yes)(Yes)(Yes)(Yes)
    files property(Yes)(Yes)3.0 (1.9)[2]10??
    multiple property(Yes)(Yes)3.6 (1.9.2)???
    indeterminate property?(Yes)(Yes)???
    list property?(Yes)(Yes)???
    formAction, formEncType, formMethod, formTarget properties(Yes)(Yes)4.0 (2)[3](Yes)(Yes)(Yes)
    formNoValidate, validationMessage, validity, willValidate properties and setCustomValidity() and checkValidity() methods.(Yes)(Yes)4.0 (2)10(Yes)(Yes)
    pattern, placeholder, required properties(Yes)(Yes)4.0 (2)(Yes)(Yes)(Yes)
    height and weight properties(Yes)(Yes)16 (16)???
    labels property14.0未实现56 (56)[1]???
    min, max, and step for <input type="number">(Yes)(Yes)Experimental, and without specific UI, since 16 (16): behind the pref dom.experimental_forms??(Yes)
    stepUp and stepDown methods?(Yes)Experimental since 16 (16): behind the pref dom.experimental_forms

    There are still differences with the latest spec: bug 835773
    ???
    min, max, and step properties for <input type="range">(Yes)(Yes)23 (23)??(Yes)
    min, max, and step properties for <input type="date">(Yes)(Yes)Experimental, and without specific UI, since 20 (20): behind the pref dom.experimental_forms??(Yes)
    min, max, and step properties for other date-related type values(Yes)(Yes)未实现??(Yes)
    mozGetFileNameArray() and mozSetFileNameArray() methods 未实现未实现3.6 (1.9.2)未实现未实现未实现
    mozSetFileArray() method 未实现未实现38 (38)未实现未实现未实现
    autocapitalize43.0未实现????
    FeatureAndroidAndroid WebviewEdgeFirefox Mobile (Gecko)IE PhoneOpera MobileSafari MobileChrome for Android
    Basic support(Yes)(Yes)(Yes)1.0 (1.0)[3](Yes)(Yes)(Yes)(Yes)
    autocapitalize未实现43.0未实现????43.0

    [1] Implemented in bug 556743.

    [2] The files property was made settable in Firefox 57 (bug 1384030).

    [3] In Firefox 56, the implementation has been updated so that the formAction property returns the correct form submission URL, as per spec, when the associated <input> is being used to submit a form (bug 1366361).

    参见

    • HTML element implementing this interface: <input>.

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

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

    发布评论

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

    词条统计

    浏览:123 次

    字数:42105

    最后编辑:7年前

    编辑次数:0 次

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