Range.setEnd() method sets the end position of a 范围 to be located at the given offset into the specified node x.Setting the end point above (higher in the document) than the start point will result in a collapsed range with the start and end points both set to the specified end position.

句法

range.setEnd(endNode, endOffset);
					

参数

endNode
节点 inside which the 范围 should end.
endOffset
An integer greater than or equal to zero representing the offset for the end of the 范围 from the start of endNode .

返回值

undefined .

异常

Exceptions are thrown as DOMException objects of the following types:

InvalidNodeTypeError
The node specified by endNode is a doctype node; range endpoints cannot be located inside a doctype node.
IndexSizeError
The value specified by endOffset is either greater than or equal to the length of the node or is less than zero.

用法注意事项

endNode 节点 类型 文本 , 注释 ,或 CDataSection ,那么 endOffset is the number of characters from the start of endNode . For other 节点 类型, endOffset is the number of child nodes between the start of the endNode .

范例

const range = document.createRange();
const endNode = document.getElementsByTagName('p').item(3);
const endOffset = endNode.childNodes.length;
range.setEnd(endNode, endOffset);
						

setEnd() is commonly used in conjunction with setStart() to fully configure a range.

规范

规范 状态 注释
DOM
The definition of 'Range.setEnd()' in that specification.
实时标准 无变化。
Document Object Model (DOM) Level 2 Traversal and Range Specification
The definition of 'Range.setEnd()' 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 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
setEnd Chrome Yes Edge 12 Firefox 4 IE 9 Opera 9 Safari Yes WebView Android Yes Chrome Android Yes Firefox Android 4 Opera Android Yes Safari iOS Yes Samsung Internet Android Yes

图例

完整支持

完整支持

另请参阅

元数据

  • 最后修改: