HTMLTextAreaElement: selectionStart 属性

Baseline 已广泛支持

此特性已相当成熟,可在许多设备和浏览器版本上使用。自 ⁨2015 年 7 月⁩以来,各浏览器均已提供此特性。

HTMLTextAreaElement 接口的 selectionStart 属性指定了 <textarea> 元素中当前文本选择的开始位置。它是一个表示选中文字开始索引的数字。它可用于检索和设置 <textarea> 中选中文字开始索引的起始位置。

当没有选中任何内容时,selectionStartselectionEnd 的值都是 <textarea> 元素内光标(插入符号)的位置。

selectionStart 设置为大于当前 selectionEnd 值的值,会将 selectionStartselectionEnd 属性都更新为该值。如果该值等于或大于 textLength,则两个属性都将设置为 textLength 属性值。

可以检索和设置该属性值,而无需 <textarea> 获得焦点,但需要元素获得焦点才能使 ::selection 伪元素匹配选中的文本。

selectionStart 设置为新值会触发 selectionchangeselect 事件。