这是一个实验中的功能
此功能某些浏览器尚在开发中,请参考浏览器兼容性表格以得到在不同浏览器中适合使用的前缀。由于该功能对应的标准文档可能被重新修订,所以在未来版本的浏览器中该功能的语法和行为可能随之改变。
语法
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); };
规范
规范 | 状态 | 备注 |
---|---|---|
Selection API Selection.type |
Working Draft | Current |
浏览器兼容性
We're converting our compatibility data into a machine-readable JSON format.
This compatibility table still uses the old format,
because we haven't yet converted the data it contains.
Find out how you can help!
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | 未实现 | (Yes) | (Yes) | ? |
Feature | Android | Edge | Firefox Mobile (Gecko) | IE Phone | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | ? | (Yes) | 未实现 | ? | ? | ? |