Selection.getRangeAt()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since March 2017.

概述

返回一个包含当前选区内容的区域对象。

语法

range = sel.getRangeAt(index)

参数

range

将返回 range 对象。

index

该参数指定需要被处理的子集编号(从零开始计数)。如果该数值被错误的赋予了大于或等于 rangeCount 结果的数字,将会产生错误。

例子

js
let ranges = [];

sel = window.getSelection();

for (var i = 0; i < sel.rangeCount; i++) {
  ranges[i] = sel.getRangeAt(i);
}
/* 在 ranges 数组的每一个元素都是一个 range 对象,
 * 对象的内容是当前选区中的一个。 */

规范

Specification
Selection API
# dom-selection-getrangeat

浏览器兼容性

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
getRangeAt

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

相关链接

  • Tree Selection (for the getRangeAt() method on the nsITreeSelection interface)