--- title: Selection.rangeCount slug: Web/API/Selection/rangeCount tags: - API - Selection translation_of: Web/API/Selection/rangeCount ---
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的值。在浏览器中选择文本,然后看看他的改变。
//打开控制台看看selection中有多少range对象。 //在Gecko浏览器,当你用鼠标在表格单元格中拖动的同时按住Ctrl,你可以选择多个range。 <table> <tr><td>a.1<td>a.2 <tr><td>b.1<td>b.2
window.setInterval(function () { console.log(window.getSelection().rangeCount); }, 1000);
Specification | Status | Comment |
---|---|---|
{{SpecName('HTML Editing', '#dom-selection-rangecount', 'Selection.rangeCount')}} | {{Spec2('HTML Editing')}} | Initial definition |
{{SpecName('Selection API', '#widl-Selection-rangeCount', 'Selection.rangeCount')}} | {{Spec2('Selection API')}} | Current |
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|
Basic support | {{CompatVersionUnknown()}} | {{CompatVersionUnknown()}} | {{CompatVersionUnknown()}} | {{CompatVersionUnknown()}} | {{CompatUnknown}} |
Feature | Android | Firefox Mobile (Gecko) | Firefox OS | IE Phone | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | {{CompatUnknown}} | {{CompatVersionUnknown()}} | 1.0 | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} |