Selection.rangeCount
Experimental: 这是一个实验中的功能
此功能某些浏览器尚在开发中,请参考浏览器兼容性表格以得到在不同浏览器中适合使用的前缀。由于该功能对应的标准文档可能被重新修订,所以在未来版本的浏览器中该功能的语法和行为可能随之改变。
The **Selection.rangeCount
**是一个返回选区 (selection) 中 range 对象数量的只读属性。
在网页使用者点击一个加载完毕的新打开的页面之前,rangeCount的值是 0。在使用者点击页面之后,rangeCount 的值变为 1,即使并没有可视的选区 (selection)。
使用者一般情况下在一次只能选择一个 range,所以通常情况下rangeCount 属性的值总为 1。脚本 (如 javascript) 可以使选区包含多个 range。
Gecko 浏览器允许跨表格单元格获得多个选区 (此处可能翻译不准). Firefox allows to select multiple ranges in the document by using Ctrl+click (unless the click within an element with display: table-cell).
用法
value = sel.rangeCount
例子
下面这个例子会每隔一秒显示一次 rangeCount 的值。在浏览器中选择文本,然后看看他的改变。
HTML Content
//打开控制台看看 selection 中有多少 range 对象。
//在 Gecko 浏览器,当你用鼠标在表格单元格中拖动的同时按住 Ctrl,你可以选择多个 range。
<table>
<tr><td>a.1<td>a.2
<tr><td>b.1<td>b.2
JavaScript Content
window.setInterval(function () {
console.log(window.getSelection().rangeCount);
}, 1000);
规范
Specification |
---|
Selection API # dom-selection-rangecount |
Browser compatibility
BCD tables only load in the browser
See also
Selection
, the interface it belongs to.