HTMLInputElement.setSelectionRange() - Web APIs 编辑

The HTMLInputElement.setSelectionRange() method sets the start and end positions of the current text selection in an <input> or <textarea> element.

Optionally, in newer browser versions, you can specify the direction in which selection should be considered to have occurred. This lets you indicate, for example, that the selection was set by the user clicking and dragging from the end of the selected text toward the beginning.

This method updates the HTMLInputElement.selectionStart, selectionEnd, and selectionDirection properties in one call.

Note that accordingly to the WHATWG forms spec selectionStart, selectionEnd properties and setSelectionRange method apply only to inputs of types text, search, URL, tel and password. Chrome, starting from version 33, throws an exception while accessing those properties and method on the rest of input types. For example, on input of type number: "Failed to read the 'selectionStart' property from 'HTMLInputElement': The input element's type ('number') does not support selection".

If you wish to select all text of an input element, you can use the HTMLInputElement.select() method instead.

Syntax

element.setSelectionRange(selectionStart, selectionEnd [, selectionDirection]);

Parameters

If selectionEnd is less than selectionStart, then both are treated as the value of selectionEnd.

selectionStart
The 0-based index of the first selected character. An index greater than the length of the element's value is treated as pointing to the end of the value.
selectionEnd
The 0-based index of the character after the last selected character. An index greater than the length of the element's value is treated as pointing to the end of the value.
selectionDirection Optional
A string indicating the direction in which the selection is considered to have been performed. Possible values:
  • "forward"
  • "backward"
  • "none" if the direction is unknown or irrelevant. Default value.

Example

Click the button in this example to select the third, fourth, and fifth characters in the text box ("zil" in the word "Mozilla").

HTML

<input type="text" id="text-box" size="20" value="Mozilla">
<button onclick="selectText()">Select text</button>

JavaScript

function selectText() {
  const input = document.getElementById('text-box');
  input.focus();
  input.setSelectionRange(2, 5);
}

Result

Specifications

SpecificationStatusComment
HTML Living Standard
The definition of 'HTMLInputElement.setSelectionRange()' in that specification.
Living StandardNo change
HTML 5.1
The definition of 'HTMLInputElement.setSelectionRange()' in that specification.
RecommendationNo change
HTML5
The definition of 'HTMLInputElement.setSelectionRange()' in that specification.
RecommendationInitial definition

Browser compatibility

BCD tables only load in the browser

See also

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

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

发布评论

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

词条统计

浏览:57 次

字数:5948

最后编辑:8年前

编辑次数:0 次

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