这是 实验性技术
检查 浏览器兼容性表格 要小心谨慎在生产中使用这之前。

Range() constructor returns a newly created 范围 object whose start and end is the global Document 对象。

句法

range = new Range()
					

范例

In this example we create a new range with the Range() constructor, and set its beginning and end positions using the Range.setStartBefore() and Range.setEndAfter() methods. We then select the range using window.getSelection() and Selection.addRange() .

HTML

<p>First paragraph.</p>
<p>Second paragraph.</p>
<p>Third paragraph.</p>
<p>Fourth paragraph.</p>
					

JavaScript

const paragraphs = document.querySelectorAll('p');
// Create new range
const range = new Range();
// Start range at second paragraph
range.setStartBefore(paragraphs[1]);
// End range at third paragraph
range.setEndAfter(paragraphs[2]);
// Get window selection
const selection = window.getSelection();
// Add range to window selection
selection.addRange(range);
					

结果

规范

规范 状态 注释
DOM
The definition of 'Range.Range()' 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
Range() 构造函数 Chrome Yes Edge ≤18 Firefox 24 IE No Opera 15 Safari Yes WebView Android Yes Chrome Android Yes Firefox Android 24 Opera Android 14 Safari iOS Yes Samsung Internet Android Yes

图例

完整支持

完整支持

不支持

不支持

实验。期望将来行为有所改变。

实验。期望将来行为有所改变。

另请参阅

元数据

  • 最后修改: