CaretPosition: thuộc tính offset
Baseline
2025
Newly available
Since December 2025, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.
Thuộc tính offset của giao diện CaretPosition trả về một số nguyên biểu thị độ lệch của vùng chọn trong nút tại vị trí con trỏ soạn thảo.
Đây sẽ là độ lệch ký tự trong nút văn bản hoặc chỉ mục của nút con được chọn trong nút phần tử.
Giá trị
Một số nguyên.
Ví dụ
Ví dụ này ghi lại offsetNode và offset của vị trí con trỏ soạn thảo khi nhấp bên trong trường nhập liệu.
html
<input aria-label="text field" value="Click inside this input field" />
js
document.querySelector("input").addEventListener("click", (event) => {
const x = event.clientX;
const y = event.clientY;
const caret = document.caretPositionFromPoint?.(x, y);
if (!caret) {
log("Not supported");
return;
}
const node = caret.offsetNode;
const offset = caret.offset;
log(`offsetNode: ${node}`);
log(`offset: ${offset}`);
});
Thông số kỹ thuật
| Specification |
|---|
| CSSOM View Module> # dom-caretposition-offset> |