Range

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

A range can be created using the createRange() method of the Document object. Range objects can also be retrieved by using the getRangeAt() (en-US) method of the Selection (en-US) object or the caretRangeFromPoint() (en-US) method of the Document object.

There also is the Range() (en-US) constructor available.

屬性

There are no inherited properties.

Range.collapsed (en-US) Read only

Returns a Boolean (en-US) indicating whether the range's start and end points are at the same position.

Range.commonAncestorContainer (en-US) Read only

Returns the deepest Node that contains the startContainer and endContainer nodes.

Range.endContainer (en-US) Read only

Returns the Node within which the Range ends.

Range.endOffset (en-US) Read only

Returns a number representing where in the endContainer the Range ends.

Range.startContainer (en-US) Read only

Returns the Node within which the Range starts.

Range.startOffset (en-US) Read only

Returns a number representing where in the startContainer the Range starts.

建構式

Range() (en-US) 實驗性質

Returns a Range object with the global Document as its start and end.

方法

There are no inherited methods.

Range.setStart() (en-US)

Sets the start position of a Range.

Range.setEnd() (en-US)

Sets the end position of a Range.

Range.setStartBefore() (en-US)

Sets the start position of a Range relative to another Node.

Range.setStartAfter() (en-US)

Sets the start position of a Range relative to another Node.

Range.setEndBefore() (en-US)

Sets the end position of a Range relative to another Node.

Range.setEndAfter() (en-US)

Sets the end position of a Range relative to another Node.

Range.selectNode() (en-US)

Sets the Range to contain the Node and its contents.

Range.selectNodeContents() (en-US)

Sets the Range to contain the contents of a Node.

Range.collapse() (en-US)

Collapses the Range to one of its boundary points.

Range.cloneContents() (en-US)

Returns a DocumentFragment copying the nodes of a Range.

Range.deleteContents() (en-US)

Removes the contents of a Range from the Document.

Range.extractContents() (en-US)

Moves contents of a Range from the document tree into a DocumentFragment.

Range.insertNode() (en-US)

Insert a Node at the start of a Range.

Range.surroundContents() (en-US)

Moves content of a Range into a new Node.

Range.compareBoundaryPoints() (en-US)

Compares the boundary points of the Range with another Range.

Range.cloneRange() (en-US)

Returns a Range object with boundary points identical to the cloned Range.

Range.detach() (en-US)

Releases the Range from use to improve performance.

Range.toString() (en-US)

Returns the text of the Range.

Range.compareNode() (en-US) 已棄用 非標準

Returns a constant representing whether the Node is before, after, inside, or surrounding the range.

Range.comparePoint() (en-US) 實驗性質

Returns -1, 0, or 1 indicating whether the point occurs before, inside, or after the Range.

Range.createContextualFragment() (en-US) 實驗性質

Returns a DocumentFragment created from a given string of code.

Range.getBoundingClientRect() (en-US) 實驗性質

Returns a DOMRect (en-US) object which bounds the entire contents of the Range; this would be the union of all the rectangles returned by range.getClientRects() (en-US).

Range.getClientRects() (en-US) 實驗性質

Returns a list of DOMRect (en-US) objects that aggregates the results of Element.getClientRects() (en-US) for all the elements in the Range.

Range.intersectsNode() (en-US) 實驗性質

Returns a boolean indicating whether the given node intersects the Range.

Range.isPointInRange() (en-US) 實驗性質

Returns a boolean indicating whether the given point is in the Range.

規範

Specification
DOM Standard
# interface-range
DOM Parsing and Serialization
# extensions-to-the-range-interface
CSSOM View Module
# extensions-to-the-range-interface

瀏覽器相容性

BCD tables only load in the browser

參見