HTMLInputElement: select() 方法
HTMLInputElement.select()
方法会选中一个 <textarea>
元素或包含文本字段的 <input>
元素中的所有文本。
语法
js
select()
参数
无。
返回值
无(undefined
)。
示例
在此示例中,单击按钮以选中 <input>
元素中的所有文本。
HTML
html
<input type="text" id="text-box" size="20" value="Hello world!" />
<button>Select text</button>
JavaScript
js
function selectText() {
const input = document.getElementById("text-box");
input.focus();
input.select();
}
document.querySelector("button").addEventListener("click", selectText);
结果
注意
调用 element.select()
不一定会使输入框获得焦点,因此它经常与 HTMLElement.focus
一起使用。
规范
规范 |
---|
HTML # dom-textarea/input-select |
浏览器兼容性
加载中…