Selection:type 属性
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2017年11月.
Selection 接口的 type 只读属性会返回用于描述当前选区类型的字符串。
值
一个用于描述当前选区类型的字符串。可能的取值包括:
示例
在这个示例中,每次创建新的选区时,事件处理函数都会被触发。console.log(selection.type) 会根据光标是停留在文本中的某一个点,还是已经选中了一个范围,来分别输出 Caret 或 Range。
js
let selection;
document.onselectionchange = () => {
console.log("已创建新的选区");
selection = document.getSelection();
console.log(selection.type);
};
规范
| Specification |
|---|
| Selection API> # dom-selection-type> |