AbstractRange
abstract interface is the base class upon which all
DOM
range types are defined. A
range
is an object that indicates the start and end points of a section of content within the document.
As an abstract interface, you will not directly instantiate an object of type
AbstractRange
. Instead, you will use the
范围
or
StaticRange
interfaces. To understand the difference between those two interfaces, and how to choose which is appropriate for your needs.
<div id="interfaceDiagram" style="display: inline-block; position: relative; width: 100%; padding-bottom: 11.666666666666666%; vertical-align: middle; overflow: hidden;"><svg style="display: inline-block; position: absolute; top: 0; left: 0;" viewbox="-50 0 600 70" preserveAspectRatio="xMinYMin meet"><a xlink:href="../API/AbstractRange" target="_top"><rect x="1" y="1" width="130" height="50" fill="#F4F7F8" stroke="#D4DDE4" stroke-width="2px" /><text x="66" y="30" font-size="12px" font-family="Consolas,Monaco,Andale Mono,monospace" fill="#4D4E53" text-anchor="middle" alignment-baseline="middle">AbstractRange</text></a></svg></div>
a:hover text { fill: #0095DD; pointer-events: all;}
collapsed
只读
true
if the range is
collapsed
. A collapsed range is one whose start position and end position are the same, resulting in a zero-character-long range.
endContainer
只读
节点
in which the end of the range, as specified by the
endOffset
property, is located.
endOffset
只读
endContainer
节点。
startContainer
只读
节点
in which the beginning of the range, as specified by the
startOffset
property, is located.
startOffset
只读
startContainer
.
AbstractRange
interface offers no methods.
All ranges of content within a
document
are described using instances of interfaces based on
AbstractRange
. There are two such interfaces:
范围
范围
interface has been around for a long time and has only recently been redefined to be based upon
AbstractRange
as the need arose to define other forms of range data.
范围
provides methods that allow you to alter the range's endpoints, as well as methods to compare ranges, detect intersections beween ranges, and so forth.
StaticRange
StaticRange
is a basic range which cannot be changed once it's been created. Specifically, as the node tree mutates and changes, the range does not. This is useful when you need to specify a range that will only be used once, since it avoids the performance and resource impact of the more complex
范围
接口。
When trying to access the contents of an element, keep in mind that the element itself is a node, but so is any text inside it. In order to set a range endpoint within the text of an element, be sure to find the text node inside the element:
let startElem = document.querySelector("p");
let endElem = startElem.querySelector("span");
let range = document.createRange();
range.setStart(startElem, 0);
range.setEnd(endElem, endElem.childNodes[0].length/2);
let contents = range.cloneContents();
document.body.appendChild(contents);
This example creates a new range,
rng
, and sets its starting point to the third child node of the first element whose class is
elementclass
. The end point is set to be the middle of the first child of the span, and then the range is used to copy the contents of the range.
In order to define a range of characters within a document in a way that is able to span across zero or more node boundaries, and which is as resilient as possible to changes to the DOM, you can't simply specify the offset to the first and last characters in the HTML . There are a few good reasons for that.
First, after your page is loaded, the browser simply isn't thinking in terms of HTML. Once it's been loaded, the page is a tree of DOM
节点
objects, so you need to specify the beginning and ending locations of a range in terms of nodes and positions within nodes.
Second, in order to support the mutability of the DOM tree as much as possible, you need a way to represent positions relative to nodes in the tree, rather than simply global positions within the entire document. By defining points within the document as offsets within a given node, those positions remain consistent with the content even as nodes are added to, removed from, or moved around within the DOM tree—within reason. There are fairly obvious limitations (such as if a node is moved to be after the endpoint of a range, or if the content of a node is heavily altered), but it's far better than nothing.
Third, using node-relative positions to define the start and end positions will generally be easier to make perform well. Rather than having to negotiate the DOM figuring out what your global offset refers to, the 用户代理 (browser) can instead go directly to the node indicated by the starting position and start from there, working its way forward until it reaches the given offset into the ending node.
To illustrate this, consider the HTML below:
<div class="container"> <div class="header"> <img src="" class="sitelogo"> <h1>The Ultimate Website</h1> </div> <article> <section class="entry" id="entry1"> <h2>Section 1: An interesting thing...</h2> <p>A <em>very</em> interesting thing happened on the way to the forum...</p> <aside class="callout"> <h2>Aside</h2> <p>An interesting aside to share with you...</p> </aside> </section> </article> <pre id="log"></pre> </div>
After loading the HTML and constructing the DOM representation of the document, the resulting DOM tree looks like this:
In this diagram, the nodes representing HTML elements are shown in green. Eah row beneath them shows the next layer of depth into the DOM tree. Blue nodes are text nodes, containing the text that gets shown onscreen. Each element's contents are linked below it in the tree, potentially spawning a series of branches below as elements include other elements and text nodes.
If you want to create a range that incorporates the contents of the
<p>
element whose contents are
"A <em>very</em> interesting thing happened on the way to the forum..."
, you can do so like this:
let pRange = document.createRange();
pRange.selectNodeContents(document.querySelector("#entry1 p"));
Since we wish to select the entire contents of the
<p>
element, including its descendants, this works perfectly.
If we wish to instead copy the text "An interesting thing..." from the
<section>
's heading (an
<h2>
element) through the end of the letters "ve" in the
<em>
within the paragraph below it, the following code would work:
let r = document.createRange();
let startNode = document.querySelector("section h2").childNodes[0];
r.setStart(startNode, 11);
let endNode = document.querySelector("#entry1 p em").childNodes[0];
r.setEnd(endNode, 2);
let fragment = r.cloneContents();
Here an interesting problem arises—we are capturing content from multiple nodes located at different levels of the DOM hierarchy, and then only part of one of them. What should the result look like?
As it turns out, the DOM specification fortunately addresses this exact issue. For example, in this case, we're calling
cloneContents()
on the range to create a new
DocumentFragment
object providing a DOM subtree which replicates the contents of the specfied range. To do this,
cloneContents()
constructs all the nodes needed to preserve the structure of the indicated range, but no more than necessary.
In this example, the start of the specified range is found within the text node below the section's heading, which means that the new
DocumentFragment
will need to contain an
<h2>
and, below it, a text node.
The range's end is located below the
p
element, so that will be needed within the new fragment. So will the text node containing the word "A", since that's included in the range. Finally, an
<em>
and a text node below it will be added below the
<p>
还。
The contents of the text nodes are then determined by the offsets into those text nodes given when calling
setStart()
and
setEnd()
. Given the offset of 11 into the heading's text, that node will contain "An interesting thing...". Similarly, the last text node will contain simply "ve", given the request for the first two characters of the ending node.
The resulting document fragment looks like this:
Notice especially that the contents of this fragment are all
below
the shared common parent of the topmost nodes within it. The parent
<section>
is not needed to replicate the cloned content, so it is isn't included.
Consider this simple HTML fragment of HTML.
<p><strong>This</strong> is a paragraph.</p>
Imagine using a
范围
to extract the word "paragraph" from this. The code to do that looks like the following:
let paraNode = document.querySelector("p");
let paraTextNode = paraNode.childNodes[1];
let range = document.createRange();
range.setStart(paraTextNode, 6);
range.setEnd(paraTextNode, paraTextNode.length-1);
let fragment = range.cloneContents();
document.body.appendChild(fragment);
First we get references to the paragraph node itelf as well as to the
second
child node within the paragraph. The first child is the
<strong>
element. The second child is the text node " is a paragraph.".
With the text node reference in hand, we create a new
范围
object by calling
createRange()
在
Document
itself. We set the starting position of the range to the sixth character of the text node's string, and the end position to the length of the text node's string minus one. This sets the range to encompass the word "paragraph".
We then finish up by calling
cloneContents()
在
范围
to create a new
DocumentFragment
object which contains the portion of the document encompassed by the range. After that, we use
appendChild()
to add that fragment at the end of the document's body, as obtained from
document.body
.
结果看起来像这样:
| 规范 | 状态 | 注释 |
|---|---|---|
|
DOM
The definition of 'AbstractRange' in that specification. |
实时标准 |
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
更新 GitHub 上的兼容性数据| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
AbstractRange
|
Chrome No | Edge 18 — 79 | Firefox 69 | IE No | Opera No | Safari No | WebView Android No | Chrome Android No | Firefox Android No | Opera Android No | Safari iOS No | Samsung Internet Android No |
collapsed
|
Chrome No | Edge 18 — 79 | Firefox 69 | IE No | Opera No | Safari No | WebView Android No | Chrome Android No | Firefox Android No | Opera Android No | Safari iOS No | Samsung Internet Android No |
endContainer
|
Chrome No | Edge 18 — 79 | Firefox 69 | IE No | Opera No | Safari No | WebView Android No | Chrome Android No | Firefox Android No | Opera Android No | Safari iOS No | Samsung Internet Android No |
endOffset
|
Chrome No | Edge 18 — 79 | Firefox 69 | IE No | Opera No | Safari No | WebView Android No | Chrome Android No | Firefox Android No | Opera Android No | Safari iOS No | Samsung Internet Android No |
startContainer
|
Chrome No | Edge 18 — 79 | Firefox 69 | IE No | Opera No | Safari No | WebView Android No | Chrome Android No | Firefox Android No | Opera Android No | Safari iOS No | Samsung Internet Android No |
startOffset
|
Chrome No | Edge 18 — 79 | Firefox 69 | IE No | Opera No | Safari No | WebView Android No | Chrome Android No | Firefox Android No | Opera Android No | Safari iOS No | Samsung Internet Android No |
完整支持
不支持