HTMLTextAreaElement: selectionStart 屬性

Baseline 已廣泛支援

此特性已相當成熟,可在許多裝置和瀏覽器版本上使用。自 ⁨2015 年 7 月⁩以來,各瀏覽器均已提供此特性。

HTMLTextAreaElement 介面的 selectionStart 屬性指定了 <textarea> 元素中當前文字選擇的開始位置。它是一個表示選中文字開始索引的數字。它可用於檢索和設定 <textarea> 中選中文字開始索引的起始位置。

當沒有選中任何內容時,selectionStartselectionEnd 的值都是 <textarea> 元素內游標(插入符號)的位置。

selectionStart 設定為大於當前 selectionEnd 值的值,會將 selectionStartselectionEnd 屬性都更新為該值。如果該值等於或大於 textLength,則兩個屬性都將設定為 textLength 屬性值。

可以檢索和設定該屬性值,而無需 <textarea> 獲得焦點,但需要元素獲得焦點才能使 ::selection 偽元素匹配選中的文字。

selectionStart 設定為新值會觸發 selectionchangeselect 事件。