aboutsummaryrefslogtreecommitdiff
path: root/files/zh-tw/web/api/range/index.html
blob: 35307c462879d13571528682fb0cc88df3914ed7 (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
---
title: Range
slug: Web/API/Range
translation_of: Web/API/Range
---
<p>{{ APIRef("DOM") }}</p>

<p><strong><code>Range</code></strong> 介面代表一個文件的片段(fragment),可以包含節點及部分的文字節點。</p>

<p>A range can be created using the {{ domxref("Document.createRange", "createRange()") }} method of the {{ domxref("Document") }} object. Range objects can also be retrieved by using the {{ domxref("Selection/getRangeAt", "getRangeAt()") }} method of the {{ domxref("Selection") }} object or the {{domxref("Document/caretRangeFromPoint", "caretRangeFromPoint()")}} method of the {{domxref("Document")}} object.</p>

<p>There also is the {{domxref("Range.Range()", "Range()")}} constructor available.</p>

<h2 id="Properties" name="Properties">屬性</h2>

<p><em>There are no inherited properties.</em></p>

<dl>
 <dt>{{domxref("Range.collapsed")}} {{readonlyInline}}</dt>
 <dd>Returns a {{domxref("Boolean")}} indicating whether the range's start and end points are at the same position.</dd>
 <dt>{{domxref("Range.commonAncestorContainer")}} {{readonlyInline}}</dt>
 <dd>Returns the deepest {{ domxref("Node") }} that contains the <code>startContainer</code> and <code>endContainer</code> nodes.</dd>
 <dt>{{domxref("Range.endContainer")}} {{readonlyInline}}</dt>
 <dd>Returns the {{ domxref("Node") }} within which the <code>Range</code> ends.</dd>
 <dt>{{domxref("Range.endOffset")}} {{readonlyInline}}</dt>
 <dd>Returns a number representing where in the <code>endContainer</code> the <code>Range</code> ends.</dd>
 <dt>{{domxref("Range.startContainer")}} {{readonlyInline}}</dt>
 <dd>Returns the {{ domxref("Node") }} within which the <code>Range</code> starts.</dd>
 <dt>{{domxref("Range.startOffset")}} {{readonlyInline}}</dt>
 <dd>Returns a number representing where in the <code>startContainer</code> the <code>Range</code> starts.</dd>
</dl>

<h2 id="建構式">建構式</h2>

<dl>
 <dt>{{ domxref("Range.Range()", "Range()") }} {{experimental_inline}}</dt>
 <dd>Returns a <code>Range</code> object with the global {{domxref("Document")}} as its start and end.</dd>
</dl>

<h2 id="Methods" name="Methods">方法</h2>

<p><em>There are no inherited methods.</em></p>

<dl>
 <dt>{{ domxref("Range.setStart()")}}</dt>
 <dd>Sets the start position of a <code>Range</code>.</dd>
 <dt>{{ domxref("Range.setEnd()")}}</dt>
 <dd>Sets the end position of a <code>Range</code>.</dd>
 <dt>{{ domxref("Range.setStartBefore()")}}</dt>
 <dd>Sets the start position of a <code>Range</code> relative to another {{ domxref("Node") }}.</dd>
 <dt>{{ domxref("Range.setStartAfter()")}}</dt>
 <dd>Sets the start position of a <code>Range</code> relative to another {{ domxref("Node") }}.</dd>
 <dt>{{ domxref("Range.setEndBefore()")}}</dt>
 <dd>Sets the end position of a <code>Range</code> relative to another {{ domxref("Node") }}.</dd>
 <dt>{{ domxref("Range.setEndAfter()")}}</dt>
 <dd>Sets the end position of a <code>Range</code> relative to another {{ domxref("Node") }}.</dd>
 <dt>{{ domxref("Range.selectNode()")}}</dt>
 <dd>Sets the <code>Range</code> to contain the {{ domxref("Node") }} and its contents.</dd>
 <dt>{{ domxref("Range.selectNodeContents()")}}</dt>
 <dd>Sets the <code>Range</code> to contain the contents of a {{ domxref("Node") }}.</dd>
 <dt>{{ domxref("Range.collapse()")}}</dt>
 <dd>Collapses the <code>Range</code> to one of its boundary points.</dd>
 <dt>{{ domxref("Range.cloneContents()")}}</dt>
 <dd>Returns a {{ domxref("DocumentFragment") }} copying the nodes of a <code>Range</code>.</dd>
 <dt>{{ domxref("Range.deleteContents()")}}</dt>
 <dd>Removes the contents of a <code>Range</code> from the {{ domxref("Document") }}.</dd>
 <dt>{{ domxref("Range.extractContents()")}}</dt>
 <dd>Moves contents of a <code>Range</code> from the document tree into a {{ domxref("DocumentFragment") }}.</dd>
 <dt>{{ domxref("Range.insertNode()")}}</dt>
 <dd>Insert a {{ domxref("Node") }} at the start of a <code>Range</code>.</dd>
 <dt>{{ domxref("Range.surroundContents()")}}</dt>
 <dd>Moves content of a <code>Range</code> into a new {{ domxref("Node") }}.</dd>
 <dt>{{ domxref("Range.compareBoundaryPoints()")}}</dt>
 <dd>Compares the boundary points of the <code>Range</code> with another <code>Range</code>.</dd>
 <dt>{{ domxref("Range.cloneRange()")}}</dt>
 <dd>Returns a <code>Range</code> object with boundary points identical to the cloned <code>Range</code>.</dd>
 <dt>{{ domxref("Range.detach()")}}</dt>
 <dd>Releases the <code>Range</code> from use to improve performance.</dd>
 <dt>{{ domxref("Range.toString()")}}</dt>
 <dd>Returns the text of the <code>Range</code>.</dd>
 <dt>{{ domxref("Range.compareNode()")}} {{ Obsolete_inline }}{{non-standard_inline}}</dt>
 <dd>Returns a constant representing whether the {{domxref("Node")}} is before, after, inside, or surrounding the range.</dd>
 <dt>{{ domxref("Range.comparePoint()")}} {{experimental_inline}}</dt>
 <dd>Returns -1, 0, or 1 indicating whether the point occurs before, inside, or after the <code>Range</code>.</dd>
 <dt>{{ domxref("Range.createContextualFragment()")}}{{experimental_inline}}</dt>
 <dd>Returns a {{ domxref("DocumentFragment") }} created from a given string of code.</dd>
 <dt>{{ domxref("Range.getBoundingClientRect()") }} {{experimental_inline}}</dt>
 <dd>Returns a {{ domxref("DOMRect") }} object which bounds the entire contents of the <code>Range</code>; this would be the union of all the rectangles returned by {{ domxref("range.getClientRects()") }}.</dd>
 <dt>{{ domxref("Range.getClientRects()") }} {{experimental_inline}}</dt>
 <dd>Returns a list of {{ domxref("DOMRect") }} objects that aggregates the results of {{ domxref("Element.getClientRects()") }} for all the elements in the <code>Range</code>.</dd>
 <dt>{{ domxref("Range.intersectsNode()")}} {{experimental_inline}}</dt>
 <dd>Returns a <code>boolean</code> indicating whether the given node intersects the <code>Range</code>.</dd>
 <dt>{{ domxref("Range.isPointInRange()")}} {{experimental_inline}}</dt>
 <dd>Returns a <code>boolean</code> indicating whether the given point is in the <code>Range</code>.</dd>
</dl>

<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('DOM WHATWG', '#interface-range', 'Range')}}</td>
   <td>{{Spec2('DOM WHATWG')}}</td>
   <td>Do not use <code>RangeException</code> anymore, use <code>DOMException</code> instead.<br>
    Made the second parameter of <code>collapse()</code> optional.<br>
    Added the methods <code>isPointInRange()</code>, <code>comparePoint()</code>, and <code>intersectsNode()</code>.<br>
    Added the constructor <code>Range()</code>.</td>
  </tr>
  <tr>
   <td>{{SpecName('DOM Parsing', '#extensions-to-the-range-interface', 'Extensions to Range')}}</td>
   <td>{{Spec2('DOM Parsing')}}</td>
   <td>Added the method <code>createContextualFragment()</code>.</td>
  </tr>
  <tr>
   <td>{{SpecName('CSSOM View', '#extensions-to-the-range-interface', 'Extensions to Range')}}</td>
   <td>{{Spec2('CSSOM View')}}</td>
   <td>Added the methods <code>getClientRects()</code> and <code>getBoundingClientRect()</code>.</td>
  </tr>
  <tr>
   <td>{{SpecName('DOM2 Traversal_Range', 'ranges.html#Level-2-Range-Interface', 'Range')}}</td>
   <td>{{Spec2('DOM2 Traversal_Range')}}</td>
   <td>Initial specification.</td>
  </tr>
 </tbody>
</table>

<h2 id="瀏覽器相容性">瀏覽器相容性</h2>

<p>{{CompatibilityTable}}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Edge</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatGeckoDesktop("1.0")}} [1]</td>
   <td>9.0</td>
   <td>9.0</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
  <tr>
   <td><code>Range()</code> constructor {{experimental_inline}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatGeckoDesktop("24.0")}}</td>
   <td>{{CompatNo}}</td>
   <td>15.0</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
  <tr>
   <td><code>compareNode()</code> {{obsolete_inline}}{{non-standard_inline()}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatGeckoDesktop("1.0")}}<br>
    Removed in {{CompatGeckoDesktop("1.9")}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
  </tr>
  <tr>
   <td><code>isPointInRange()</code>, and <code>comparePoint()</code>{{experimental_inline}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatNo}}</td>
   <td>15.0</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td><code>intersectsNode()</code> {{experimental_inline}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatGeckoDesktop("17.0")}} [2]</td>
   <td>{{CompatNo}}</td>
   <td>15.0</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td><code>getClientRects()</code> and <code>getBoundingClientRect()</code>{{experimental_inline}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatGeckoDesktop("2.0")}}</td>
   <td>9</td>
   <td>15.0</td>
   <td>5</td>
  </tr>
  <tr>
   <td><code>createContextualFragment()</code>{{experimental_inline}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>11</td>
   <td>15.0</td>
   <td>{{CompatUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Edge</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatGeckoMobile("1.0")}} [1]</td>
   <td>9.0</td>
   <td>9.0</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<p>[1] Starting with Gecko 13.0 {{ geckoRelease("13.0") }} the <code>Range</code> object throws a {{ domxref("DOMException") }} as defined in DOM 4, instead of a <code>RangeException</code> defined in prior specifications.</p>

<p>[2] Gecko supported it up to Gecko 1.9, then removed it until Gecko 17 where it was reimplemented, matching the spec.</p>

<h2 id="參見">參見</h2>

<ul>
 <li><a href="/en-US/docs/DOM/DOM_Reference" title="/en-US/docs/DOM/DOM_Reference">The DOM interfaces index</a></li>
</ul>