翻译正在进行中。
这是一个实验中的功能
此功能某些浏览器尚在开发中,请参考浏览器兼容性表格以得到在不同浏览器中适合使用的前缀。由于该功能对应的标准文档可能被重新修订,所以在未来版本的浏览器中该功能的语法和行为可能随之改变。
构造函数 Range()
返回一个新创建的 Range
对象,新创建的对象属于全局 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');
// 创建 Range 对象
const range = new Range();
// Range 起始位置在段落2
range.setStartBefore(paragraphs[1]);
// Range 结束位置在段落3
range.setEndAfter(paragraphs[2]);
// 获取 selection 对象
const selection = window.getSelection();
// 添加光标选择的范围
selection.addRange(range);
Result
规范
Specification | Status | Comment |
---|---|---|
DOM Range.Range() |
Living Standard | Initial definition. |
浏览器兼容性
We're converting our compatibility data into a machine-readable JSON format.
This compatibility table still uses the old format,
because we haven't yet converted the data it contains.
Find out how you can help!
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | (Yes) | 24.0 (24.0) | 未实现 | 15.0 | (Yes) |
Feature | Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|
Basic support | (Yes) | 24.0 (24.0) | 未实现 | 15.0 | (Yes) |