Selection: addRange() 方法
語法
js
addRange(range)
引數
返回值
無(undefined)。
示例
注意: 目前只有 Firefox 支援多個選區範圍,如果選擇區域已包含一個範圍,其他瀏覽器將不會新增新的範圍。
HTML
html
<p>
I <strong>insist</strong> that you <strong>try</strong> selecting the
<strong>strong words</strong>.
</p>
<button>Select strong words</button>
JavaScript
js
let button = document.querySelector("button");
button.addEventListener("click", () => {
const selection = window.getSelection();
const strongElems = document.getElementsByTagName("strong");
if (selection.rangeCount > 0) {
selection.removeAllRanges();
}
for (const node of strongElems) {
const range = document.createRange();
range.selectNode(node);
selection.addRange(range);
}
});
結果
規範
| 規範 |
|---|
| Selection API # dom-selection-addrange |
瀏覽器相容性
載入中…
另見
Selection,該方法所屬的介面