blob: 4628fd85603f660c28d27c42c75ef425977c6a09 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
---
title: Selection.rangeCount
slug: Web/API/Selection/rangeCount
tags:
- API
- Selection
translation_of: Web/API/Selection/rangeCount
---
<div>
<div>
<div>{{ ApiRef("DOM") }}{{SeeCompatTable}}</div>
</div>
</div>
<p>The <strong><code>Selection.rangeCount</code></strong>是一个返回选区(selection)中range对象数量的只读属性。</p>
<p>在网页使用者点击一个加载完毕的新打开的页面之前,<strong>rangeCount</strong><font face="Consolas, Liberation Mono, Courier, monospace">的值是0</font>。在使用者点击页面之后,<font face="Consolas, Liberation Mono, Courier, monospace">rangeCount的值变为1,即使并没有可视的选区(selection)。</font></p>
<p>使用者一般情况下在一次只能选择一个range ,所以通常情况下<strong>rangeCount属性</strong>的值总为1。脚本(如javascript)可以使选区包含多个range。</p>
<p>Gecko 浏览器允许跨表格单元格获得多个选区(此处可能翻译不准). Firefox allows to select multiple ranges in the document by using Ctrl+click (unless the click within an element with display: table-cell).</p>
<h2 id="Syntax" name="Syntax">用法</h2>
<pre class="syntaxbox"><em>value</em> =<em> sel</em>.rangeCount
</pre>
<h2 id="例子">例子</h2>
<p>下面这个例子会每隔一秒显示一次rangeCount的值。在浏览器中选择文本,然后看看他的改变。</p>
<h3 id="HTML_Content">HTML Content</h3>
<pre class="brush: html">//打开控制台看看selection中有多少range对象。
//在Gecko浏览器,当你用鼠标在表格单元格中拖动的同时按住Ctrl,你可以选择多个range。
<table>
<tr><td>a.1<td>a.2
<tr><td>b.1<td>b.2
</pre>
<h3 id="JavaScript_Content">JavaScript Content</h3>
<pre class="brush: js">window.setInterval(function () {
console.log(window.getSelection().rangeCount);
}, 1000);
</pre>
<h2 id="规范">规范</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Specification</th>
<th scope="col">Status</th>
<th scope="col">Comment</th>
</tr>
<tr>
<td>{{SpecName('HTML Editing', '#dom-selection-rangecount', 'Selection.rangeCount')}}</td>
<td>{{Spec2('HTML Editing')}}</td>
<td>Initial definition</td>
</tr>
<tr>
<td>{{SpecName('Selection API', '#widl-Selection-rangeCount', 'Selection.rangeCount')}}</td>
<td>{{Spec2('Selection API')}}</td>
<td>Current</td>
</tr>
</tbody>
</table>
<h2 id="Browser_compatibility" name="Browser_compatibility">Browser compatibility</h2>
{{Compat("api.Selection.rangeCount")}}
<h2 id="See_also" name="See_also">See also</h2>
<ul>
<li>{{domxref("Selection")}}, the interface it belongs to.</li>
</ul>
|