HTMLTextAreaElement: selectionStart property
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
Thuộc tính selectionStart của giao diện HTMLTextAreaElement chỉ định vị trí bắt đầu của vùng chọn văn bản hiện tại trong phần tử <textarea>. Đây là một số đại diện cho chỉ mục bắt đầu của văn bản được chọn. Thuộc tính này có thể được dùng để vừa lấy vừa đặt vị trí bắt đầu của văn bản được chọn trong <textarea>.
Khi không có gì được chọn, giá trị của cả selectionStart và selectionEnd là vị trí của con trỏ nhập (caret) bên trong phần tử <textarea>.
Đặt selectionStart thành giá trị lớn hơn giá trị hiện tại của selectionEnd sẽ cập nhật cả hai thuộc tính selectionStart và selectionEnd thành giá trị đó. Nếu giá trị đó bằng hoặc lớn hơn textLength, cả hai thuộc tính đều được đặt thành giá trị của thuộc tính textLength.
Giá trị thuộc tính có thể được lấy và đặt mà không cần <textarea> có focus, nhưng phần tử vẫn cần có focus để pseudo-element ::selection khớp với văn bản được chọn.
Đặt selectionStart thành một giá trị mới sẽ kích hoạt các sự kiện selectionchange và select.
Giá trị
Một số không âm.
Ví dụ
const textarea = document.getElementById("text-box");
const start = textarea.selectionStart;
Đặc tả kỹ thuật
| Specification |
|---|
| HTML> # dom-textarea/input-selectionstart> |
Tương thích trình duyệt
Xem thêm
<textarea>HTMLTextAreaElementHTMLTextAreaElement.selectionEndHTMLTextAreaElement.selectionDirectionHTMLTextAreaElement.textLengthHTMLTextAreaElement.select()HTMLTextAreaElement.setSelectionRange()HTMLTextAreaElement.setRangeText()HTMLInputElement.selectionStartSelection- Pseudo-element
::selection