--- title: HTMLInputElement slug: Web/API/HTMLInputElement tags: - API - HTML DOM - Interface - NeedsNewLayout - NeedsTranslation - Reference - TopicStub translation_of: Web/API/HTMLInputElement ---
HTMLInputElement
接口提供了特定的属性和方法(继承自常规的{{domxref("HTMLElement", "HTML元素")}}接口)用于管理输入元素的布局和外观。
{{InheritanceDiagram(600,120)}}
form {{readonlyInline}} |
{{domxref("HTMLFormElement")}} object: 返回一个父表单元素的引用。 |
formAction |
string : 返回/ 设置 元素的 {{ htmlattrxref("formaction", "input") }} 属性, 包含处理元素提交信息程序的URI. 这会重写父表单的 {{ htmlattrxref("action", "form") }} 属性. |
formEncType |
string : 返回/ 设置 元素的 {{ htmlattrxref("formenctype", "input") }} 属性, 包含将表单提交到服务器的内容类型. 这会重写父表单的 {{ htmlattrxref("enctype", "form") }} 属性. |
formMethod |
string : 返回/ 设置 元素的 {{ htmlattrxref("formmethod", "input") }} 属性, 包含浏览器用于提交表单的HTTP方法. 这会重写父表单的 {{ htmlattrxref("method", "form") }} 属性. |
formNoValidate |
boolean : 返回/ 设置 元素的 {{ htmlattrxref("formnovalidate", "input") }} 属性, 表示在表单提交时不对其进行验证. 这会重写父表单的 {{ htmlattrxref("novalidate", "form") }} 属性. |
formTarget |
string : 返回/ 设置 元素的 {{ htmlattrxref("formtarget", "input") }} 属性, 包含一个名称或关键字, 表示在提交表单后接收响应的显示位置. 这会重写父表单的 {{ htmlattrxref("target", "form") }} 属性. |
name |
string : 返回/ 设置 元素的 {{ htmlattrxref("name", "input") }} 属性, 包含其名称. |
type |
string: 返回/ 设置 元素的 {{ htmlattrxref("type", "input") }} 属性, 包含其显示类型. 查看 {{ HTMLElement("input") }} 的 {{ htmlattrxref("type", "input") }} 属性可用值. |
disabled |
boolean : 返回/ 设置 元素的 {{ htmlattrxref("disabled", "input") }} 属性, 表示是否禁用 {{ HTMLElement("input") }}. 该元素的值将不会被提交. 也可以查看 {{ htmlattrxref("readOnly", "input") }} |
autofocus |
boolean: 返回/ 设置 元素的 {{ htmlattrxref("autofocus", "input") }} 属性, 指定的 {{ HTMLElement("input") }} 在页面加载时应当具有输入焦点, 例如通过键入不同的控件. 在文档中只有一个表单元素可以拥有 {{htmlattrxref("autofocus","input")}} 属性. 如果 {{htmlattrxref("type","input")}} 属性被设置为 hidden 则无效(即不可为隐藏控件设置自动焦点). |
required |
boolean : 返回/ 设置 元素的 {{ htmlattrxref("required", "input") }} 属性, 表示用户必填项. |
value |
string: 返回/ 设置 当前控件的值.
提示: 如果用户输入与预期不同,可能会返回空. |
validity {{readonlyInline}} |
{{domxref("ValidityState")}} object: Returns the validity state that this element is in. |
validationMessage {{readonlyInline}} |
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 ({{htmlattrxref("willValidate","input")}} is false), or it satisfies its constraints. |
willValidate {{readonlyInline}} |
{{jsxref("Boolean")}}: Indicates whether the element is a candidate for constraint validation. It is false if any conditions bar it from constraint validation. |
alt |
string : 返回/ 设置 元素的 {{ htmlattrxref("alt", "input") }} 属性, 包含 alternative text to use when {{htmlattrxref("type","input")}} is image. |
height |
string : 返回/ 设置 元素的 {{ htmlattrxref("height", "input") }} 属性, which defines the height of the image displayed for the button, if the value of {{htmlattrxref("type","input")}} is image . |
src |
string: 返回/ 设置 元素的 {{ htmlattrxref("src", "input") }} 属性, which specifies a URI for the location of an image to display on the graphical submit button, if the value of {{htmlattrxref("type","input")}} is image ; otherwise it is ignored. |
width |
string: 返回/ 设置 the document's {{ htmlattrxref("width", "input") }} 属性, which defines the width of the image displayed for the button, if the value of {{htmlattrxref("type","input")}} is image . |
accept |
string : Returns / Sets 元素的 {{ htmlattrxref("accept", "input") }} 属性, 包含 comma-separated list of file types accepted by the server when {{htmlattrxref("type","input")}} is file . |
allowdirs {{non-standard_inline}} |
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. |
files |
Returns/accepts a {{domxref("FileList")}} object, which contains a list of {{domxref("File")}} objects representing the files selected for upload. |
{{domxref("HTMLInputElement.webkitdirectory", "webkitdirectory")}} {{Non-standard_inline}} | boolean: Returns the {{htmlattrxref("webkitdirectory", "input")}} 属性; if true, the file system picker interface only accepts directories instead of files. |
{{domxref("HTMLInputElement.webkitEntries", "webkitEntries")}} {{Non-standard_inline}} | Array of {{domxref("FileSystemEntry")}} objects describing the currently-selected files or directories. |
autocomplete |
string : 返回/ 设置 元素的 {{htmlattrxref("autocomplete", "input")}} 属性, indicating whether the value of the control can be automatically completed by the browser. Ignored if the value of the {{htmlattrxref("type","input")}} 属性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 |
maxLength |
long : 返回/ 设置 元素的 {{ htmlattrxref("maxlength", "input") }} 属性, 包含 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.) |
size |
unsigned long : 返回/ 设置 元素的 {{ htmlattrxref("size", "input") }} 属性, 包含 size of the control. This value is in pixels unless the value of {{htmlattrxref("type","input")}} is text or password , in which case, it is an integer number of characters. Applies only when {{htmlattrxref("type","input")}} is set to text , search , tel , url , email , or password ; otherwise it is ignored. |
pattern |
string : 返回/ 设置 元素的 {{ htmlattrxref("pattern", "input") }} 属性, 包含 a regular expression that the control's value is checked against. Use the {{htmlattrxref("title","input")}} 属性to describe the pattern to help the user. This 属性applies when the value of the {{htmlattrxref("type","input")}} 属性is text , search , tel , url or email ; otherwise it is ignored. |
placeholder |
string : 返回/ 设置 元素的 {{ htmlattrxref("placeholder", "input") }} 属性, 包含 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 {{htmlattrxref("type","input")}} 属性is text , search , tel , url or email ; otherwise it is ignored. |
readOnly |
boolean : 返回/ 设置 元素的 {{ htmlattrxref("readonly", "input") }} 属性, indicating that the user cannot modify the value of the control.{{HTMLVersionInline(5)}}This is ignored if the value of the {{htmlattrxref("type","input")}} 属性is hidden , range , color , checkbox , radio , file , or a button type. |
min |
string : 返回/ 设置 元素的 {{ htmlattrxref("min", "input") }} 属性, 包含 the minimum (numeric or date-time) value for this item, which must not be greater than its maximum ({{htmlattrxref("max","input")}} 属性) value. |
max |
string : 返回/ 设置 元素的 {{ htmlattrxref("max", "input") }} 属性, 包含 the maximum (numeric or date-time) value for this item, which must not be less than its minimum (min 属性) value. |
selectionStart |
unsigned 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 {{HTMLElement("input")}} element. |
selectionEnd |
unsigned 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. |
selectionDirection |
string : 返回/ 设置 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." |
defaultValue |
string: 返回/ 设置 the default value as originally specified in the HTML that created this object. |
dirName |
string: 返回/ 设置 the directionality of the element. |
accessKey |
string : 返回 a string 包含 a single character that switches input focus to the control when pressed. |
list {{readonlyInline}} |
{{domxref("HTMLElement")}} object: 返回 the element pointed by the {{ htmlattrxref("list", "input") }} 属性. The property may be null if no HTML element found in the same tree. |
multiple |
boolean : 返回/ 设置 元素的 {{ htmlattrxref("multiple", "input") }} 属性, indicating whether more than one value is possible (e.g., multiple files). |
files |
{{domxref("FileList")}} array: 返回the list of selected files. |
{{domxref("HTMLInputElement.labels")}} {{readonlyInline}} | {{domxref("NodeList")}} array: 返回a list of {{ HTMLElement("label") }} elements that are labels for this element. |
step |
string: 返回/ 设置 元素的 {{ htmlattrxref("step", "input") }} 属性, which works with {{htmlattrxref("min","input")}} and {{htmlattrxref("max","input")}} 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. |
valueAsDate |
{{jsxref("Date")}} object: Returns / Sets the value of the element, interpreted as a date, or null if conversion is not possible. |
valueAsNumber |
double : 返回the value of the element, interpreted as one of the following, in order:
|
autocapitalize {{experimental_inline}} |
string: 定义 the capitalization behavior for user input. Valid values are none , off , characters , words , or sentences . |
string:
represents the alignment of the element. Use CSS instead.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() |
|
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 {{jsxref("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 {{event("invalid")}} event at the element. It returns true if the element is not a candidate for constraint validation, or if it satisfies its constraints. |
mozSetFileNameArray()
which can be used in frame scripts: a chrome script can open files as File objects and send them via message manager.Specification | Status | Comment |
---|---|---|
{{SpecName('HTML WHATWG', "forms.html#the-input-element", "HTMLInputElement")}} | {{Spec2('HTML WHATWG')}} | No change from {{SpecName("HTML5 W3C")}} |
{{SpecName('HTML5 W3C', "forms.html#the-input-element", "HTMLInputElement")}} | {{Spec2('HTML5 W3C')}} | Technically, the tabindex and accesskey properties, as well as the blur(), click(), and focus() methods, are now defined on {{domxref("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(). |
{{SpecName('DOM2 HTML', 'html.html#ID-6043025', 'HTMLInputElement')}} | {{Spec2('DOM2 HTML')}} | The size property is now an unsigned long. The type property must be entirely given in lowercase characters. |
{{SpecName('DOM1', 'level-one-html.html#ID-6043025', 'HTMLInputElement')}} | {{Spec2('DOM1')}} | Initial definition. |
{{CompatibilityTable}}
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|---|
Basic support | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatGeckoDesktop(1.0)}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} |
autocomplete & autofocus properties | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} |
files property | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatGeckoDesktop(1.9)}}[2] | 10 | {{CompatUnknown}} | {{CompatUnknown}} |
multiple property | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatGeckoDesktop("1.9.2")}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} |
indeterminate property | {{CompatUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} |
list property | {{CompatUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} |
formAction, formEncType, formMethod, formTarget properties | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatGeckoDesktop(2)}}[3] | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} |
formNoValidate, validationMessage, validity, willValidate properties and setCustomValidity() and checkValidity() methods. | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatGeckoDesktop(2)}} | 10 | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} |
pattern, placeholder, required properties | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatGeckoDesktop(2)}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} |
height and weight properties | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatGeckoDesktop(16)}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} |
labels property |
{{CompatChrome(14.0)}} | {{CompatNo}} | {{CompatGeckoDesktop("56")}}[1] | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} |
min, max, and step for <input type="number"> | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | Experimental, and without specific UI, since {{CompatGeckoDesktop(16)}}: behind the pref dom.experimental_forms | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatVersionUnknown}} |
stepUp and stepDown methods | {{CompatUnknown}} | {{CompatVersionUnknown}} | Experimental since {{CompatGeckoDesktop(16)}}: behind the pref dom.experimental_forms There are still differences with the latest spec: {{bug(835773)}} |
{{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} |
min, max, and step properties for <input type="range"> | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatGeckoDesktop(23)}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatVersionUnknown}} |
min, max, and step properties for <input type="date"> | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | Experimental, and without specific UI, since {{CompatGeckoDesktop(20)}}: behind the pref dom.experimental_forms | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatVersionUnknown}} |
min, max, and step properties for other date-related type values | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatNo}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatVersionUnknown}} |
mozGetFileNameArray() and mozSetFileNameArray() methods {{non-standard_inline}} | {{CompatNo}} | {{CompatNo}} | {{CompatGeckoDesktop("1.9.2")}} | {{CompatNo}} | {{CompatNo}} | {{CompatNo}} |
mozSetFileArray() method {{non-standard_inline}} | {{CompatNo}} | {{CompatNo}} | {{CompatGeckoDesktop("38")}} | {{CompatNo}} | {{CompatNo}} | {{CompatNo}} |
autocapitalize | {{CompatChrome(43.0)}} | {{CompatNo}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} |
Feature | Android | Android Webview | Edge | Firefox Mobile (Gecko) | IE Phone | Opera Mobile | Safari Mobile | Chrome for Android |
---|---|---|---|---|---|---|---|---|
Basic support | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatGeckoMobile(1.0)}}[3] | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} |
autocapitalize | {{CompatNo}} | {{CompatChrome(43.0)}} | {{CompatNo}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatChrome(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)}}).