Selection: phương thức deleteFromDocument()
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
Phương thức deleteFromDocument() của giao diện Selection gọi phương thức Range.deleteContents() trên Range đang được chọn.
Cú pháp
js
deleteFromDocument()
Tham số
Không có.
Giá trị trả về
Không có (undefined).
Ví dụ
Ví dụ này cho phép bạn xóa văn bản và/hoặc nút đã chọn bằng cách bấm vào một nút. Khi bấm nút, phương thức Window.getSelection() lấy nội dung đã chọn, và phương thức deleteFromDocument() sẽ xóa nó.
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();
}
Kết quả
Thông số kỹ thuật
| Thông số kỹ thuật |
|---|
| Selection API> # dom-selection-deletefromdocument> |
Tương thích trình duyệt
Xem thêm
Selection, giao diện định nghĩa phương thức này