Selection:removeRange() 方法
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2023年9月.
Selection.removeRange() 方法用于从当前选区中移除一个范围。
语法
js
removeRange(range)
参数
range-
一个将被从选区中移除的 Range 对象。
返回值
无(undefined)。
示例
js
/* 在程序层面,可以选中多个范围。
* 此操作将移除除第一个范围外的所有其他范围。*/
const s = window.getSelection();
if (s.rangeCount > 1) {
for (let i = 1; i < s.rangeCount; i++) {
s.removeRange(s.getRangeAt(i));
}
}
规范
| Specification |
|---|
| Selection API> # dom-selection-removerange> |
浏览器兼容性
参见
- 所属接口:
Selection。