diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/api/htmlinputelement | |
parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip |
initial commit
Diffstat (limited to 'files/zh-cn/web/api/htmlinputelement')
7 files changed, 1099 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/htmlinputelement/index.html b/files/zh-cn/web/api/htmlinputelement/index.html new file mode 100644 index 0000000000..77ad4ed2c7 --- /dev/null +++ b/files/zh-cn/web/api/htmlinputelement/index.html @@ -0,0 +1,648 @@ +--- +title: HTMLInputElement +slug: Web/API/HTMLInputElement +tags: + - API + - HTML DOM + - Interface + - NeedsNewLayout + - NeedsTranslation + - Reference + - TopicStub +translation_of: Web/API/HTMLInputElement +--- +<div>{{ APIRef("HTML DOM") }}</div> + +<p><strong><code>HTMLInputElement</code></strong> 接口提供了特定的属性和方法(继承自常规的{{domxref("HTMLElement", "HTML元素")}}接口)用于管理输入元素的布局和外观。</p> + +<p>{{InheritanceDiagram(600,120)}}</p> + +<h2 id="Properties" name="Properties">属性</h2> + +<table class="standard-table"> + <caption>与父表单相关的属性</caption> + <thead> + </thead> + <tbody> + <tr> + <td><code>form </code>{{readonlyInline}}</td> + <td><em><code>{{domxref("HTMLFormElement")}} object:</code></em> 返回一个父表单元素的引用。</td> + </tr> + <tr> + <td><code>formAction</code></td> + <td><em><code>string</code>: </em><strong>返回/ 设置</strong> 元素的 {{ htmlattrxref("formaction", "input") }} 属性, 包含处理元素提交信息程序的URI. 这会重写父表单的 {{ htmlattrxref("action", "form") }} 属性.</td> + </tr> + <tr> + <td><code>formEncType</code></td> + <td><em><code>string</code>:</em> <strong>返回/ 设置</strong> 元素的 {{ htmlattrxref("formenctype", "input") }} 属性, 包含将表单提交到服务器的内容类型. 这会重写父表单的 {{ htmlattrxref("enctype", "form") }} 属性.</td> + </tr> + <tr> + <td><code>formMethod</code></td> + <td><em><code>string</code>:</em> <strong>返回/ 设置</strong> 元素的 {{ htmlattrxref("formmethod", "input") }} 属性, 包含浏览器用于提交表单的HTTP方法. 这会重写父表单的 {{ htmlattrxref("method", "form") }} 属性.</td> + </tr> + <tr> + <td><code>formNoValidate</code></td> + <td><em><code>boolean</code>:</em> <strong>返回/ 设置</strong> 元素的 {{ htmlattrxref("formnovalidate", "input") }} 属性, 表示在表单提交时不对其进行验证. 这会重写父表单的 {{ htmlattrxref("novalidate", "form") }} 属性.</td> + </tr> + <tr> + <td><code>formTarget</code></td> + <td><em><code>string</code>:</em> <strong>返回/ 设置</strong> 元素的 {{ htmlattrxref("formtarget", "input") }} 属性, 包含一个名称或关键字, 表示在提交表单后接收响应的显示位置. 这会重写父表单的 {{ htmlattrxref("target", "form") }} 属性.</td> + </tr> + </tbody> +</table> + +<table class="standard-table"> + <caption>可用于任何非隐藏输入元素的属性</caption> + <tbody> + <tr> + <td><code>name</code></td> + <td><em><code>string</code>:</em> <strong>返回/ 设置</strong> 元素的 {{ htmlattrxref("name", "input") }} 属性, 包含<font face="consolas, Liberation Mono, courier, monospace"><span style="background-color: rgba(220, 220, 220, 0.5);">其</span></font>名称.</td> + </tr> + <tr> + <td><code>type</code></td> + <td><code><em>string:</em></code> <strong>返回/ 设置</strong> 元素的 {{ htmlattrxref("type", "input") }} 属性, 包含<font face="consolas, Liberation Mono, courier, monospace"><span style="background-color: rgba(220, 220, 220, 0.5);">其</span></font>显示类型. 查看 {{ HTMLElement("input") }} 的 {{ htmlattrxref("type", "input") }} 属性可用值.</td> + </tr> + <tr> + <td><code>disabled</code></td> + <td><em><code>boolean</code>:</em> <strong>返回/ 设置</strong> 元素的 {{ htmlattrxref("disabled", "input") }} 属性, 表示是否禁用 {{ HTMLElement("input") }}. 该元素的值将不会被提交. 也可以查看 {{ htmlattrxref("readOnly", "input") }} </td> + </tr> + <tr> + <td><code>autofocus</code></td> + <td><code><em>boolean:</em></code> <strong>返回/ 设置</strong> 元素的 {{ htmlattrxref("autofocus", "input") }} 属性, 指定的 {{ HTMLElement("input") }} 在页面加载时应当具有输入焦点, 例如通过键入不同的控件. 在文档中只有一个表单元素可以拥有 {{htmlattrxref("autofocus","input")}} 属性. 如果 {{htmlattrxref("type","input")}} 属性被设置为 <code>hidden</code> 则无效(即不可为隐藏控件设置自动焦点).</td> + </tr> + <tr> + <td><code>required</code></td> + <td><em><code>boolean</code>:</em> <strong>返回/ 设置</strong> 元素的 {{ htmlattrxref("required", "input") }} 属性, 表示用户必填项.</td> + </tr> + <tr> + <td><code>value</code></td> + <td><code><em>string:</em></code> <strong>返回/ 设置</strong> 当前控件的值. + <p class="note"><strong>提示:</strong> 如果用户输入与预期不同,可能会返回空.</p> + </td> + </tr> + <tr> + <td><code>validity</code> {{readonlyInline}}</td> + <td><code><em>{{domxref("ValidityState")}} object:</em></code> Returns the validity state that this element is in.</td> + </tr> + <tr> + <td><code>validationMessage</code> {{readonlyInline}}</td> + <td><code><em>string:</em></code> <strong>Returns</strong> 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.</td> + </tr> + <tr> + <td><code>willValidate </code>{{readonlyInline}}</td> + <td><em><code>{{jsxref("Boolean")}}:</code></em> <strong>Indicates</strong> whether the element is a candidate for constraint validation. It is false if any conditions bar it from constraint validation.</td> + </tr> + </tbody> +</table> + +<table class="standard-table"> + <caption><a id="Properties_checkbox_radio" name="Properties_checkbox_radio"></a>仅适用于类型为"checkbox" 或 "radio"元素的属性</caption> + <tbody> + <tr> + <td><code>checked</code></td> + <td> <code><em>boolean:</em></code> <strong>返回/ 设置</strong> 当前选中状态,当控件{{htmlattrxref("type","input")}} 是 <code>checkbox</code> 或 <code>radio时</code>.</td> + </tr> + <tr> + <td><code>defaultChecked</code></td> + <td><em><code>boolean:</code></em> <strong>返回/ 设置</strong> the default state of a radio button or checkbox as originally specified in HTML that created this object.</td> + </tr> + <tr> + <td><code><a id="indeterminate" name="indeterminate"></a>indeterminate</code></td> + <td><code><em>boolean:</em></code> <strong>indicates</strong> that the checkbox is neither on nor off. Changes the appearance to resemble a third state. Does not affect the value of the <strong>checked</strong> 属性, and clicking the checkbox will set the value to false.</td> + </tr> + </tbody> +</table> + +<table class="standard-table"> + <caption>仅适用于类型为"image"元素的属性</caption> + <tbody> + <tr> + <td><code>alt</code></td> + <td><em><code>string</code>: </em><strong>返回/ 设置</strong> 元素的 {{ htmlattrxref("alt", "input") }} 属性, 包含 alternative text to use when {{htmlattrxref("type","input")}} is <code>image.</code></td> + </tr> + <tr> + <td><code>height</code></td> + <td><em><code>string</code>: </em><strong>返回/ 设置</strong> 元素的 {{ htmlattrxref("height", "input") }} 属性, which defines the height of the image displayed for the button, if the value of {{htmlattrxref("type","input")}} is <code>image</code>.</td> + </tr> + <tr> + <td><code>src</code></td> + <td><code><em>string:</em></code> <strong>返回/ 设置</strong> 元素的 {{ 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 <code>image</code>; otherwise it is ignored.</td> + </tr> + <tr> + <td><code>width</code></td> + <td><code><em>string:</em></code> <strong>返回/ 设置</strong> 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 <code>image</code>.</td> + </tr> + </tbody> +</table> + +<table class="standard-table"> + <caption>仅适用于类型为"file"元素的属性</caption> + <tbody> + <tr> + <td><code>accept</code></td> + <td><em><code>string</code>: </em><strong>Returns / Sets</strong> 元素的 {{ htmlattrxref("accept", "input") }} 属性, 包含 comma-separated list of file types accepted by the server when {{htmlattrxref("type","input")}} is <code>file</code>.</td> + </tr> + <tr> + <td><code>allowdirs</code> {{non-standard_inline}}</td> + <td>boolean: Part of the non-standard Directory Upload API; <strong>indicates</strong> 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.</td> + </tr> + <tr> + <td><a id="files_prop" name="files_prop"><code>files</code></a></td> + <td><strong>Returns/accepts</strong> a {{domxref("FileList")}} object, which contains a list of {{domxref("File")}} objects representing the files selected for upload.</td> + </tr> + <tr> + <td>{{domxref("HTMLInputElement.webkitdirectory", "webkitdirectory")}} {{Non-standard_inline}}</td> + <td>boolean:<strong> Returns</strong> the {{htmlattrxref("webkitdirectory", "input")}} 属性; if true, the file system picker interface only accepts directories instead of files.</td> + </tr> + <tr> + <td>{{domxref("HTMLInputElement.webkitEntries", "webkitEntries")}} {{Non-standard_inline}}</td> + <td>Array of {{domxref("FileSystemEntry")}} objects <strong>describing</strong> the currently-selected files or directories.</td> + </tr> + </tbody> +</table> + +<table class="standard-table"> + <caption>仅适用于文本或数字输入元素的属性</caption> + <tbody> + <tr> + <td><code>autocomplete</code></td> + <td><em><code>string</code>: </em><strong>返回/ 设置</strong> 元素的 {{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 <code>hidden</code>, <code>checkbox</code>, <code>radio</code>, <code>file</code>, or a button type (<code>button</code>, <code>submit</code>, <code>reset</code>, <code>image</code>). Possible values are:<br> + "on": the browser can autocomplete the value using previously stored value<br> + "off": the user must explicity enter a value</td> + </tr> + <tr> + <td><code>maxLength</code></td> + <td><em><code>long</code>:</em> <strong>返回/ 设置</strong> 元素的 {{ htmlattrxref("maxlength", "input") }} 属性, 包含 the <u><strong>maximum <strong>l</strong>ength of characters</strong></u> (in Unicode code points) that the value can have. (If you set this to a negative number, an exception will be thrown.)</td> + </tr> + <tr> + <td><code>size</code></td> + <td><em><code>unsigned long</code>:</em> <strong>返回/ 设置 </strong>元素的 {{ htmlattrxref("size", "input") }} 属性, 包含 <u><strong>size of the control</strong></u>. This value is in pixels unless the value of {{htmlattrxref("type","input")}} is <code>text</code> or <code>password</code>, in which case, it is an integer number of characters. Applies only when {{htmlattrxref("type","input")}} is set to <code>text</code>, <code>search</code>, <code>tel</code>, <code>url</code>, <code>email</code>, or <code>password</code>; otherwise it is ignored.</td> + </tr> + <tr> + <td><code>pattern</code></td> + <td><em><code>string</code>:</em> <strong>返回/ 设置</strong> 元素的 {{ htmlattrxref("pattern", "input") }} 属性, 包含 a <u><strong>regular expression</strong></u> 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 <code>text</code>, <code>search</code>, <code>tel</code>, <code>url</code> or <code>email</code>; otherwise it is ignored.</td> + </tr> + <tr> + <td><code>placeholder</code></td> + <td><em><code>string</code>: </em><strong>返回/ 设置 </strong>元素的 {{ 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 <code>text</code>, <code>search</code>, <code>tel</code>, <code>url</code> or <code>email</code>; otherwise it is ignored.</td> + </tr> + <tr> + <td><code>readOnly</code></td> + <td><em><code>boolean</code>:</em><strong> 返回/ 设置</strong> 元素的 {{ htmlattrxref("readonly", "input") }} 属性, indicating that the user cannot modify the value of the control.<br> + {{HTMLVersionInline(5)}}This is ignored if the value of the {{htmlattrxref("type","input")}} 属性is <code>hidden</code>, <code>range</code>, <code>color</code>, <code>checkbox</code>, <code>radio</code>, <code>file</code>, or a button type.</td> + </tr> + <tr> + <td><code>min</code></td> + <td><em><code>string</code>:</em> <strong>返回/ 设置</strong> 元素的 {{ 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.</td> + </tr> + <tr> + <td><code>max</code></td> + <td><em><code>string</code>:</em> <strong>返回/ 设置</strong> 元素的 {{ htmlattrxref("max", "input") }} 属性, 包含 the maximum (numeric or date-time) value for this item, which must not be less than its minimum (<strong>min</strong> 属性) value.</td> + </tr> + <tr> + <td><code>selectionStart</code></td> + <td><em><code>unsigned long</code>:</em> <strong>返回/ 设置</strong> 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.</td> + </tr> + <tr> + <td><code>selectionEnd</code></td> + <td><em><code>unsigned long</code>:</em> <strong>返回/ 设置 </strong>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.</td> + </tr> + <tr> + <td><code>selectionDirection</code></td> + <td><em><code>string</code>:</em> <strong>返回/ 设置</strong> the direction in which selection occurred. Possible values are:<br> + <code>"forward"</code> if selection was performed in the start-to-end direction of the current locale,<br> + <code>"backward"</code> for the opposite direction,<br> + <code>"none"</code> if the direction is unknown."</td> + </tr> + </tbody> +</table> + +<table class="standard-table"> + <caption>未分类的属性</caption> + <tbody> + <tr> + <td><code>defaultValue</code></td> + <td><em><code>string:</code></em> <strong>返回/ 设置</strong> the default value as originally specified in the HTML that created this object.</td> + </tr> + <tr> + <td><code>dirName</code></td> + <td><em><code>string:</code></em> <strong>返回/ 设置 </strong>the directionality of the element.</td> + </tr> + <tr> + <td><code>accessKey</code></td> + <td><em><code>string</code>: <strong>返回</strong></em> a string 包含 a single character that switches input focus to the control when pressed.</td> + </tr> + <tr> + <td><code>list</code> {{readonlyInline}}</td> + <td><em><code>{{domxref("HTMLElement")}} object:</code></em> <strong>返回</strong> the element pointed by the {{ htmlattrxref("list", "input") }} 属性. The property may be <code>null</code> if no HTML element found in the same tree.</td> + </tr> + <tr> + <td><code>multiple</code></td> + <td><em><code>boolean</code>:</em> <strong>返回/ 设置</strong> 元素的 {{ htmlattrxref("multiple", "input") }} 属性, indicating whether more than one value is possible (e.g., multiple files).</td> + </tr> + <tr> + <td><code>files</code></td> + <td><em><code>{{domxref("FileList")}} array:</code></em> <strong>返回</strong>the list of selected files.</td> + </tr> + <tr> + <td>{{domxref("HTMLInputElement.labels")}} {{readonlyInline}}</td> + <td><code><em>{{domxref("NodeList")}} array:</em></code> <strong>返回</strong>a list of {{ HTMLElement("label") }} elements that are labels for this element.</td> + </tr> + <tr> + <td><code>step</code></td> + <td><code><em>string:</em></code> <strong>返回/ 设置</strong> 元素的 {{ htmlattrxref("step", "input") }} 属性, which works with<strong> </strong>{{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 <code>any</code> or a positive floating point number. If this is not set to <code>any</code>, the control accepts only values at multiples of the step value greater than the minimum.</td> + </tr> + <tr> + <td><code>valueAsDate</code></td> + <td><code><em>{{jsxref("Date")}} object:</em></code> Returns / Sets the value of the element, interpreted as a date, or <code>null</code> if conversion is not possible.</td> + </tr> + <tr> + <td><code>valueAsNumber</code></td> + <td><code>double</code>: <strong>返回</strong>the value of the element, interpreted as one of the following, in order: + <ul> + <li>a time value</li> + <li>a number</li> + <li>NaN if conversion is impossible</li> + </ul> + </td> + </tr> + <tr> + <td><code>autocapitalize</code> {{experimental_inline}}</td> + <td><code><em>string:</em></code> <strong>定义</strong> the capitalization behavior for user input. Valid values are <code>none</code>, <code>off</code>, <code>characters</code>, <code>words</code>, or <code>sentences</code>.</td> + </tr> + </tbody> +</table> + +<dl> + <dt>{{domxref("HTMLInputElement.align")}} {{obsolete_inline}}</dt> + <dd><code><em>string:</em></code> <strong>represents</strong> the alignment of the element. <em>Use CSS instead.</em></dd> + <dt>{{domxref("HTMLInputElement.useMap")}} {{ obsolete_inline }}</dt> + <dd><code><em>string:</em></code> <strong>represents</strong> a client-side image map.</dd> +</dl> + +<h2 id="方法">方法</h2> + +<table class="standard-table"> + <tbody> + <tr> + <td><code>focus()</code></td> + <td>Focus on the input element; keystrokes will subsequently go to this element.</td> + </tr> + <tr> + <td><code>blur()</code></td> + <td>Removes focus from input; keystrokes will subsequently go nowhere.</td> + </tr> + <tr> + <td><code><a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/select">select()</a></code></td> + <td>Selects the input text in the element, and focuses it so the user can subsequently replace the whole entry.</td> + </tr> + <tr> + <td><code><a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/click">click()</a></code></td> + <td>Simulates a click on the element.</td> + </tr> + <tr> + <td><code><a href="/en-US/docs/Web/API/HTMLInputElement/setSelectionRange">setSelectionRange()</a></code></td> + <td>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.</td> + </tr> + <tr> + <td><code>setRangeText()</code></td> + <td> + <dl> + <dd> + <p>Replaces a range of text with the new text. If the <var>start</var> and <var>end</var> 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:</p> + + <dl> + <dt>"<code>select</code>"</dt> + <dd>Selects the newly inserted text.</dd> + <dt>"<code>start</code>"</dt> + <dd>Moves the selection to just before the inserted text.</dd> + <dt>"<code>end</code>"</dt> + <dd>Moves the selection to just after the selected text.</dd> + <dt>"<code>preserve</code>"</dt> + <dd>Attempts to preserve the selection. This is the default.</dd> + </dl> + </dd> + </dl> + </td> + </tr> + <tr> + <td><code>setCustomValidity()</code></td> + <td>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.</td> + </tr> + <tr> + <td><code>checkValidity()</code></td> + <td>Returns a {{jsxref("Boolean")}} that is <code>false</code> 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 <code>true</code> if the element is not a candidate for constraint validation, or if it satisfies its constraints.</td> + </tr> + </tbody> +</table> + +<dl> + <dt>{{domxref("HTMLInputElement.stepDown()")}}</dt> + <dd>Decrements the {{htmlattrxref("value","input")}} by ({{htmlattrxref("step","input")}} * n), where n defaults to 1 if not specified. Throws an INVALID_STATE_ERR exception: + <ul> + <li>if the method is not applicable to for the current {{htmlattrxref("type","input")}} value,</li> + <li>if the element has no {{htmlattrxref("step","input")}} value,</li> + <li>if the {{htmlattrxref("value","input")}} cannot be converted to a number,</li> + <li>if the resulting value is above the {{htmlattrxref("max","input")}} or below the {{htmlattrxref("min","input")}}. </li> + </ul> + </dd> + <dt>{{domxref("HTMLInputElement.stepUp()")}}</dt> + <dd>Increments the {{htmlattrxref("value","input")}} by ({{htmlattrxref("step","input")}} * n), where n defaults to 1 if not specified. Throws an INVALID_STATE_ERR exception: + <ul> + <li>if the method is not applicable to for the current {{htmlattrxref("type","input")}} value.,</li> + <li>if the element has no {{htmlattrxref("step","input")}} value,</li> + <li>if the {{htmlattrxref("value","input")}} cannot be converted to a number,</li> + <li>if the resulting value is above the {{htmlattrxref("max","input")}} or below the {{htmlattrxref("min","input")}}.</li> + </ul> + </dd> + <dt>{{domxref("HTMLInputElement.mozSetFileArray()")}}{{non-standard_inline}}</dt> + <dd>Sets the files selected on the input to the given array of {{domxref("File")}} objects. This is an alternative to <code>mozSetFileNameArray()</code> which can be used in frame scripts: a chrome script can <a href="/en-US/docs/Extensions/Using_the_DOM_File_API_in_chrome_code">open files as File objects</a> and send them via <a href="/en-US/Firefox/Multiprocess_Firefox/The_message_manager">message manager</a>.</dd> + <dt>{{domxref("HTMLInputElement.mozGetFileNameArray()")}}{{non-standard_inline}}</dt> + <dd>Returns an array of all the file names from the input.</dd> + <dt>{{domxref("HTMLInputElement.mozSetFileNameArray()")}}{{non-standard_inline}}</dt> + <dd>Sets the filenames for the files selected on the input. Not for use in <a href="/en-US/Firefox/Multiprocess_Firefox/Limitations_of_frame_scripts">frame scripts</a>, because it accesses the file system.</dd> + <dd> + <ul> + </ul> + </dd> +</dl> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>{{SpecName('HTML WHATWG', "forms.html#the-input-element", "HTMLInputElement")}}</td> + <td>{{Spec2('HTML WHATWG')}}</td> + <td>No change from {{SpecName("HTML5 W3C")}}</td> + </tr> + <tr> + <td>{{SpecName('HTML5 W3C', "forms.html#the-input-element", "HTMLInputElement")}}</td> + <td>{{Spec2('HTML5 W3C')}}</td> + <td>Technically, the tabindex and accesskey properties, as well as the blur(), click(), and focus() methods, are now defined on {{domxref("HTMLElement")}}.<br> + The following properties are now obsolete: align and useMap.<br> + 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.<br> + The following methods have been added: checkValidity(), setCustomValidity(), setSelectionRange(), stepUp(), and stepDown().</td> + </tr> + <tr> + <td>{{SpecName('DOM2 HTML', 'html.html#ID-6043025', 'HTMLInputElement')}}</td> + <td>{{Spec2('DOM2 HTML')}}</td> + <td>The size property is now an unsigned long. The type property must be entirely given in lowercase characters.</td> + </tr> + <tr> + <td>{{SpecName('DOM1', 'level-one-html.html#ID-6043025', 'HTMLInputElement')}}</td> + <td>{{Spec2('DOM1')}}</td> + <td>Initial definition.</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<p>{{CompatibilityTable}}</p> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Chrome</th> + <th>Edge</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari (WebKit)</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatGeckoDesktop(1.0)}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + <tr> + <td>autocomplete & autofocus properties</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + <tr> + <td>files property</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatGeckoDesktop(1.9)}}<sup>[2]</sup></td> + <td>10</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + </tr> + <tr> + <td>multiple property</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatGeckoDesktop("1.9.2")}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + </tr> + <tr> + <td>indeterminate property</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + </tr> + <tr> + <td>list property</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + </tr> + <tr> + <td>formAction, formEncType, formMethod, formTarget properties</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatGeckoDesktop(2)}}<sup>[3]</sup></td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + <tr> + <td>formNoValidate, validationMessage, validity, willValidate properties and setCustomValidity() and checkValidity() methods.</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatGeckoDesktop(2)}}</td> + <td>10</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + <tr> + <td>pattern, placeholder, required properties</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatGeckoDesktop(2)}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + <tr> + <td>height and weight properties</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatGeckoDesktop(16)}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + </tr> + <tr> + <td><code>labels</code> property</td> + <td>{{CompatChrome(14.0)}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatGeckoDesktop("56")}}<sup>[1]</sup></td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + </tr> + <tr> + <td>min, max, and step for <input type="number"></td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>Experimental, and without specific UI, since {{CompatGeckoDesktop(16)}}: behind the pref dom.experimental_forms</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + <tr> + <td>stepUp and stepDown methods</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>Experimental since {{CompatGeckoDesktop(16)}}: behind the pref dom.experimental_forms<br> + <br> + There are still differences with the latest spec: {{bug(835773)}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + </tr> + <tr> + <td>min, max, and step properties for <input type="range"></td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatGeckoDesktop(23)}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + <tr> + <td>min, max, and step properties for <input type="date"></td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>Experimental, and without specific UI, since {{CompatGeckoDesktop(20)}}: behind the pref dom.experimental_forms</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + <tr> + <td>min, max, and step properties for other date-related type values</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + <tr> + <td>mozGetFileNameArray() and mozSetFileNameArray() methods {{non-standard_inline}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatGeckoDesktop("1.9.2")}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + </tr> + <tr> + <td>mozSetFileArray() method {{non-standard_inline}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatGeckoDesktop("38")}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + </tr> + <tr> + <td>autocapitalize</td> + <td>{{CompatChrome(43.0)}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Android</th> + <th>Android Webview</th> + <th>Edge</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Phone</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + <th>Chrome for Android</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatGeckoMobile(1.0)}}<sup>[3]</sup></td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + <tr> + <td>autocapitalize</td> + <td>{{CompatNo}}</td> + <td>{{CompatChrome(43.0)}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatUnknown}}</td> + <td><span style="font-size: 12px; line-height: 16.3636360168457px;">{{CompatUnknown}}</span></td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatChrome(43.0)}}</td> + </tr> + </tbody> +</table> +</div> + +<p>[1] Implemented in {{bug("556743")}}.</p> + +<p>[2] The <code>files</code> property was made settable in Firefox 57 ({{bug(1384030)}}).</p> + +<p>[3] In Firefox 56, the implementation has been updated so that the <code>formAction</code> property returns the correct form submission URL, as per spec, when the associated <code><input></code> is being used to submit a form ({{bug(1366361)}}).</p> + +<h2 id="参见">参见</h2> + +<ul> + <li>HTML element implementing this interface: {{ HTMLElement("input") }}.</li> +</ul> diff --git a/files/zh-cn/web/api/htmlinputelement/invalid_event/index.html b/files/zh-cn/web/api/htmlinputelement/invalid_event/index.html new file mode 100644 index 0000000000..6f1f97a94d --- /dev/null +++ b/files/zh-cn/web/api/htmlinputelement/invalid_event/index.html @@ -0,0 +1,109 @@ +--- +title: 'HTMLInputElement: invalid event' +slug: Web/API/HTMLInputElement/invalid_event +tags: + - Constraint validation + - Event + - 事件 + - 参考 + - 提交 + - 表单 +translation_of: Web/API/HTMLInputElement/invalid_event +--- +<div>{{APIRef}}</div> + +<p>若一个可提交元素在检查有效性时,不符合对它的约束条件,则会触发 <strong><code>invalid</code></strong> 事件。</p> + +<table class="properties"> + <tbody> + <tr> + <th>冒泡</th> + <td>否</td> + </tr> + <tr> + <th>可取消</th> + <td>是</td> + </tr> + <tr> + <th>接口</th> + <td>{{DOMxRef("Event")}}</td> + </tr> + <tr> + <th>事件处理程序属性</th> + <td>{{domxref("GlobalEventHandlers.oninvalid")}}</td> + </tr> + </tbody> +</table> + +<p>这个事件可用于展示提交表单时所出现的问题的概览。当表单提交时,若任一表单控件无效,则会触发 <code>invalid</code> 事件。对可提交元素有效性的检查是在提交父元素 {{HtmlElement("form")}} 之前或调用父元素 <code><form></code> 或元素自己的 <a href="/en-US/docs/HTML/Forms_in_HTML#Constraint_Validation_API" title="/en-US/docs/HTML/Forms_in_HTML#Constraint_Validation_API"><code>checkValidity()</code></a> 方法之后。</p> + +<p>这个事件不会在 {{Event("blur")}} 事件中触发。</p> + +<h2 id="示例">示例</h2> + +<p>如果表单提交时有无效值,检查可提交元素时发现了错误,则 <code>invalid</code> 事件会在那个无效元素上触发。在这个例子中,当输入无效值触发 <code>invalid</code> 事件时,这个无效值被记录下来。</p> + +<h3 id="HTML">HTML</h3> + +<pre class="brush: html"><form action="#"> + <ul> + <li><label>Enter an integer between 1 and 10: <input type="number" min="1" max="10" required></label></li> + <li><input type="submit" value="submit"></li> + </ul> +</form><p id="log"></p></pre> + +<h3 id="JavaScript">JavaScript</h3> + +<pre class="brush: js">const input = document.querySelector('input') +const log = document.getElementById('log') + +input.addEventListener('invalid', logValue) + +function logValue(e) { + log.textContent += e.target.value +}</pre> + +<h3 id="结果">结果</h3> + +<p><iframe class="live-sample-frame sample-code-frame" frameborder="0" id="frame_Examples" src="https://mdn.mozillademos.org/en-US/docs/Web/API/HTMLInputElement/invalid_event$samples/Examples?revision=1602988"></iframe></p> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">规范</th> + <th scope="col">状态</th> + <th scope="col">注释</th> + </tr> + <tr> + <td>{{ SpecName('HTML WHATWG', 'forms.html#the-constraint-validation-api', 'Invalid event') }}</td> + <td>{{Spec2('HTML WHATWG')}}</td> + <td></td> + </tr> + <tr> + <td>{{ SpecName('HTML5.1', 'sec-forms.html#the-constraint-validation-api', 'Invalid event') }}</td> + <td>{{Spec2('HTML5.1')}}</td> + <td></td> + </tr> + <tr> + <td>{{ SpecName('HTML5 W3C', 'forms.html#the-constraint-validation-api', 'Invalid event') }}</td> + <td>{{Spec2('HTML5 W3C')}}</td> + <td></td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + + + +<p>{{Compat("api.HTMLInputElement.invalid_event")}}</p> + +<h2 id="参见">参见</h2> + +<ul> + <li>HTML {{HtmlElement("form")}} 元素</li> + <li>相关事件:{{Event("submit")}}</li> + <li><a href="/en-US/docs/Web/CSS/:invalid">CSS <code>:invalid</code> 伪类</a></li> +</ul> diff --git a/files/zh-cn/web/api/htmlinputelement/labels/index.html b/files/zh-cn/web/api/htmlinputelement/labels/index.html new file mode 100644 index 0000000000..5015d41a0d --- /dev/null +++ b/files/zh-cn/web/api/htmlinputelement/labels/index.html @@ -0,0 +1,71 @@ +--- +title: HTMLInputElement.labels +slug: Web/API/HTMLInputElement/labels +translation_of: Web/API/HTMLInputElement/labels +--- +<div>{{APIRef("DOM")}}</div> + +<p> <code><strong>HTMLInputElement.labels</strong></code> 为只读属性,它返回一个{{domxref("NodeList")}} 实例,绑定当前的{{HTMLElement("input")}} 节点的{{HTMLElement("label")}} 元素。</p> + + + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox">var <var>labelElements</var> = <var>input</var>.labels; +</pre> + +<h3 id="Return_value">Return value</h3> + +<p> {{domxref("NodeList")}} 包含 <code><label></code> 元素和 <code><input></code> 元素.</p> + +<h2 id="实例">实例</h2> + +<h3 id="HTML">HTML</h3> + +<pre class="brush: html"><label id="label1" for="test">Label 1</label> +<input id="test"/> +<label id="label2" for="test">Label 2</label> +</pre> + +<h3 id="JavaScript">JavaScript</h3> + +<pre class="brush: js">window.addEventListener("DOMContentLoaded", function() { + const input = document.getElementById("test"); + for(var i = 0; i < input.labels.length; i++) { + console.log(input.labels[i].textContent); // "Label 1" and "Label 2" + } +});</pre> + +<p>{{EmbedLiveSample("Example", "100%", 30)}}</p> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName("HTML WHATWG", "forms.html#dom-lfe-labels", "labels")}}</td> + <td>{{Spec2("HTML WHATWG")}}</td> + <td>No change</td> + </tr> + <tr> + <td>{{SpecName("HTML5 W3C", "forms.html#dom-lfe-labels", "labels")}}</td> + <td>{{Spec2("HTML5 W3C")}}</td> + <td>Initial definition</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容">浏览器兼容</h2> + +<div> +<div class="hidden">本页兼容表是由结构化数据生成的。如果你乐意为数据做贡献,请点击<a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> 给我们发送请求。</div> + +<p>{{Compat("api.HTMLInputElement.labels")}}</p> +</div> diff --git a/files/zh-cn/web/api/htmlinputelement/mozsetfilenamearray/index.html b/files/zh-cn/web/api/htmlinputelement/mozsetfilenamearray/index.html new file mode 100644 index 0000000000..6ba774d00e --- /dev/null +++ b/files/zh-cn/web/api/htmlinputelement/mozsetfilenamearray/index.html @@ -0,0 +1,48 @@ +--- +title: HTMLInputElement.mozSetFileNameArray +slug: Web/API/HTMLInputElement/mozSetFileNameArray +translation_of: Web/API/HTMLInputElement +--- +<div> +<div> +<div> +<div>{{APIRef("HTML DOM")}}</div> +</div> +</div> +{{gecko_minversion_header("1.9.2")}}</div> + +<h3 id="Summary" name="Summary">概述</h3> + +<p>设置一个HTML <code>input</code>元素中选中的若干文件的路径以及文件名.</p> + +<div class="note"><strong>注:</strong> 该方法是Gecko私有的方法,在其他浏览器中不可用,且是个特权方法,不能在普通网页中使用.</div> + +<h3 id="Syntax" name="Syntax">语法</h3> + +<pre class="eval">inputElement.mozSetFileNameArray(<em>aFileNames, </em><em>aLength</em>); +</pre> + +<h3 id="Parameters" name="Parameters">参数</h3> + +<ul> + <li><code>aFileNames</code> 指定给该元素的若干个文件的文件名路径以及文件名.</li> + <li><code>aLength </code>需要指定文件的个数(<code>通常是</code>数组<code>aFileNames</code>的长度).</li> +</ul> + +<h3 id="Example" name="Example">示例</h3> + +<pre class="brush: js">var fileArray = {"/foo/bar.txt", "/foo/foosball.txt"}; + +inputElement.mozSetFileNameArray(fileArray, fileArray.length); +</pre> + +<h3 id="Specification" name="Specification">规范</h3> + +<p>非标准,Mozilla私有方法.</p> + +<h3 id="相关链接">相关链接</h3> + +<ul> + <li><a href="/zh-CN/DOM/Input" title="zh-CN/DOM/Input"><code>Input</code></a></li> + <li><a href="/zh-CN/DOM/Input.mozGetFileNameArray" title="zh-CN/DOM/Input.mozGetFileNameArray"><code>Input.mozGetFileNameArray</code></a></li> +</ul> diff --git a/files/zh-cn/web/api/htmlinputelement/multiple/index.html b/files/zh-cn/web/api/htmlinputelement/multiple/index.html new file mode 100644 index 0000000000..937c52a93f --- /dev/null +++ b/files/zh-cn/web/api/htmlinputelement/multiple/index.html @@ -0,0 +1,44 @@ +--- +title: HTMLInputElement.multiple +slug: Web/API/HTMLInputElement/multiple +translation_of: Web/API/HTMLInputElement/multiple +--- +<div>{{ APIRef("HTML DOM") }}</div> + +<p><strong><code>HTMLInputElement.multiple</code></strong> 属性表示一个input是否可以有多个值。目前只有火狐支持 <code><input type="file"></code>存有多个值。</p> + +<h2 id="实例">实例</h2> + +<pre class="brush:js">// fileInput is a <input type=file multiple> +let fileInput = document.getElementById('myfileinput'); + +if (fileInput.multiple == true) { + + for (let i = 0; i < fileInput.files.length; i++) { + // Loop fileInput.files + } + +// Only one file available +} else { + let file = fileInput.files.item(0); +} +</pre> + +<h2 id="See_also">See also</h2> + +<ul> + <li><a href="/en/DOM/FileList" title="en/DOM/FileList">FileList</a></li> + <li><a class="link-https" href="https://bugzilla.mozilla.org/show_bug.cgi?id=523771" title="https://bugzilla.mozilla.org/show_bug.cgi?id=523771">Bug 523771</a> - <span class="bz_default_hidden" id="summary_alias_container" style="display: inline;"> <span id="short_desc_nonedit_display">Support <input type=file multiple></span> </span></li> +</ul> + +<h2 id="Specification">Specification</h2> + +<ul> + <li><a class="external" href="http://www.whatwg.org/specs/web-apps/current-work/multipage/common-input-element-attributes.html#attr-input-multiple" title="http://www.whatwg.org/specs/web-apps/current-work/multipage/common-input-element-attributes.html#attr-input-multiple">The multiple attribute</a> (HTML 5 working draft)</li> +</ul> + +<h2 id="Browser_compatibility">Browser compatibility</h2> + + + +<p>{{Compat("api.HTMLInputElement.multiple")}}</p> diff --git a/files/zh-cn/web/api/htmlinputelement/select/index.html b/files/zh-cn/web/api/htmlinputelement/select/index.html new file mode 100644 index 0000000000..5da551d19f --- /dev/null +++ b/files/zh-cn/web/api/htmlinputelement/select/index.html @@ -0,0 +1,69 @@ +--- +title: HTMLInputElement.select() +slug: Web/API/HTMLInputElement/select +translation_of: Web/API/HTMLInputElement/select +--- +<div>{{ APIRef("HTML DOM") }}</div> + +<p><strong><code>HTMLInputElement.select()</code></strong> 方法选中一个 {{HTMLElement("textarea")}} 元素或者一个带有 text 字段的 {{HTMLElement("input")}} 元素里的所有内容。</p> + +<h2 id="Syntax" name="Syntax">语法</h2> + +<pre class="syntaxbox">element.select()</pre> + +<h2 id="示例">示例</h2> + +<p>点击这个例子的按钮以选中所有在<code><input></code>元素中的文字</p> + +<p>HTML</p> + +<pre class="brush: html"><code><input type="text" id="text-box" size="20" value="Hello world!"> +<button onclick="selectText()">Select text</button></code></pre> + +<p>JavaScript</p> + +<pre class="brush: js"><code>function selectText() { + const input = document.getElementById('text-box'); + input.focus(); + input.select(); +}</code></pre> + +<h3 id="结果">结果</h3> + +<p>{{EmbedLiveSample("示例")}}</p> + +<h2 id="注意">注意</h2> + +<p>调用 <code>element.select()</code> 并不一定会使得该 input 元素被 focused,所以它经常和 {{domxref("HTMLElement.focus()")}} 一起使用。</p> + +<h2 id="Specification" name="Specification">规范</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">规范</th> + <th scope="col">状态</th> + <th scope="col">注释</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('HTML WHATWG', 'forms.html#dom-textarea/input-select', 'select')}}</td> + <td>{{Spec2('HTML WHATWG')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<p>{{Compat("api.HTMLInputElement.select")}}</p> + +<h2 id="相关链接">相关链接</h2> + +<ul> + <li>{{ HTMLElement("input") }}</li> + <li>{{ HTMLElement("textarea") }}</li> + <li>{{ domxref("HTMLInputElement") }}</li> + <li>{{ domxref("HTMLInputElement.setSelectionRange") }}</li> +</ul> diff --git a/files/zh-cn/web/api/htmlinputelement/setselectionrange/index.html b/files/zh-cn/web/api/htmlinputelement/setselectionrange/index.html new file mode 100644 index 0000000000..1dfc53223b --- /dev/null +++ b/files/zh-cn/web/api/htmlinputelement/setselectionrange/index.html @@ -0,0 +1,110 @@ +--- +title: HTMLInputElement.setSelectionRange() +slug: Web/API/HTMLInputElement/setSelectionRange +tags: + - API + - HTML DOM + - HTMLInputElement + - 参考 + - 文本选择 + - 方法 +translation_of: Web/API/HTMLInputElement/setSelectionRange +--- +<div>{{APIRef("HTML DOM")}}</div> + +<p><strong><code>HTMLInputElement.setSelectionRange</code> </strong>方法用于设定{{HTMLElement("input")}} 或 {{HTMLElement("textarea")}} 元素中当前选中文本的起始和结束位置。</p> + +<p>在较新的浏览器中,你可以通过一个可选的 selectionDirection 来指定文本选中的方向。比如通过点击和拖动从结束位置往起始位置选中一个字符串。</p> + +<p>每次调用这个这个方法都会更新 <code>HTMLInputElement</code> 的 <code>selectionStart</code>, <code>selectionEnd</code> 和 <code>selectionDirection</code> 属性。</p> + +<p>要注意的是,在 <a href="https://html.spec.whatwg.org/multipage/forms.html#concept-input-apply">WHATWG forms spec</a> 中,<code>selectionStart</code>, <code>selectionEnd</code> 属性和 <code>setSelectionRange</code> 方法只能应用于类型为文本、搜索、链接、电话号码和密码的输入。Chrome 从版本 33 开始会在访问其余类型的这些属性和方法时抛出异常。例如,输入类型为数字时会抛出:“不能从'HTMLInputElement'中读取'selectionStart'属性:输入元素的类型('number')不支持选择(Failed to read the 'selectionStart' property from 'HTMLInputElement': The input element's type ('number') does not support selection)”。</p> + +<p>如果你希望<strong>全选</strong>输入元素中的文本,你可以使用 <a href="https://wiki.developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/select">HTMLInputElement.select()</a> 方法。</p> + +<h2 id="语法">语法</h2> + +<pre><em>element</em>.setSelectionRange(<em>selectionStart</em>, <em>selectionEnd</em> [, <em>selectionDirection</em>]);</pre> + +<h3 id="参数">参数</h3> + +<p>如果 <code>selectionEnd</code> 小于 <code>selectionStart</code>,则二者都会被看作 <code>selectionEnd</code>。</p> + +<dl> + <dt><code>selectionStart</code></dt> + <dd>被选中的第一个字符的位置索引,从0开始。如果这个值比元素的 <code>value</code> 长度还大,则会被看作 <code>value</code> 最后一个位置的索引。</dd> + <dt><code>selectionEnd</code></dt> + <dd>被选中的最后一个字符的 <em>下一个</em> 位置索引。如果这个值比元素的value长度还大,则会被看作value最后一个位置的索引。</dd> + <dt><code>selectionDirection</code> {{optional_inline}}</dt> + <dd>一个表示选择方向的字符串,可能的值有:</dd> +</dl> + +<ul> + <li><code>"forward"</code></li> + <li><code>"backward"</code></li> + <li><code>"none"</code> 默认值,表示方向未知或不相关。</li> +</ul> + +<h2 id="示例">示例</h2> + +<p>在这个示例中,按下按钮以选择文本框中第三、四、五个字符(即“Mozilla”中的“zil”)。</p> + +<h3 id="HTML">HTML</h3> + +<pre><input type="text" id="text-box" size="20" value="Mozilla"> +<button onclick="selectText()">Select text</button> +</pre> + +<h3 id="JavaScript">JavaScript</h3> + +<pre>function selectText() { + const input = document.getElementById('text-box'); + input.focus(); + input.setSelectionRange(2, 5); +}</pre> + +<h3 id="结果">结果</h3> + +<p><iframe class="live-sample-frame sample-code-frame" frameborder="0" id="frame_Example" src="https://mdn.mozillademos.org/en-US/docs/Web/API/HTMLInputElement/setSelectionRange$samples/Example?revision=1557137"></iframe></p> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">规范</th> + <th scope="col">状态</th> + <th scope="col">注释</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName("HTML WHATWG", "forms.html#dom-textarea/input-setselectionrange", "HTMLInputElement.setSelectionRange()")}}</td> + <td>{{Spec2("HTML WHATWG")}}</td> + <td>No change</td> + </tr> + <tr> + <td>{{SpecName("HTML5.1", "forms.html#dom-textarea/input-setselectionrange", "HTMLInputElement.setSelectionRange()")}}</td> + <td>{{Spec2("HTML5.1")}}</td> + <td>No change</td> + </tr> + <tr> + <td>{{SpecName("HTML5 W3C", "forms.html#dom-textarea/input-setselectionrange", "HTMLInputElement.setSelectionRange()")}}</td> + <td>{{Spec2("HTML5 W3C")}}</td> + <td>Initial definition</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<p>{{Compat("api.HTMLInputElement.setSelectionRange")}}</p> + +<h2 id="相关内容">相关内容</h2> + +<ul> + <li>{{HTMLElement("input")}}</li> + <li>{{HTMLElement("textarea")}}</li> + <li>{{domxref("HTMLInputElement")}}</li> + <li>{{domxref("Selection")}}</li> +</ul> |