Selection: deleteFromDocument() 方法
Selection 接口的 deleteFromDocument() 方法会在选中的 Range 上调用 Range.deleteContents() 方法。
语法
js
deleteFromDocument()
参数
无。
返回值
无(undefined)。
示例
此示例允许您通过单击按钮来删除选定的文本和/或节点。单击按钮后,Window.getSelection() 方法会获取选中的内容,然后 deleteFromDocument() 方法将其删除。
HTML
html
<p>Try highlighting some of the elements in these paragraphs.</p>
<p>
Once you do, you can remove the selected content by clicking the button below.
</p>
<h2>All of the selected text/nodes will be removed.</h2>
<button>Delete selected text</button>
JavaScript
js
let button = document.querySelector("button");
button.addEventListener("click", deleteSelection);
function deleteSelection() {
let selection = window.getSelection();
selection.deleteFromDocument();
}
结果
规范
| 规范 |
|---|
| Selection API # dom-selection-deletefromdocument |
浏览器兼容性
加载中…
另见
Selection,定义此方法的接口