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 March 2017.

实验性: 这是一项实验性技术
在将其用于生产之前,请仔细检查浏览器兼容性表格

**type**是 Selection 接口的只读属性,其返回的是DOMString即描述当前选择的类型。

语法

value = sel.type

Value

DOMString 描述的是当前选择的类型。可能的值为:

  • None: 当前没有选择。
  • Caret: 选区已折叠(即 光标在字符之间,并未处于选中状态)。
  • Range: 选择的是一个范围。

例子

在下面的示例中,回调函数将在每次进行新的选择时触发。 console.log(selection.type) 将会输出 Caret 或者 Range ,其输出值取决于插入标记是放置在文本中的单个点还是已选择范围。

var selection;

document.onselectionchange = function() {
  console.log('New selection made');
  selection = document.getSelection();
  console.log(selection.type);
};

规范

Specification
Selection API
# dom-selection-type

浏览器兼容性

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
type

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

参考