the*_*edp 11 javascript select highlight
假设我使用鼠标在页面上突出显示一些文字.如何使用JavaScript删除所有突出显示的文本?
谢谢.
小智 40
我对这个问题的理解有点不同了.我相信您想知道如何从文档中删除所选文本,在这种情况下您可以使用:
function deleteSelection() {
if (window.getSelection) {
// Mozilla
var selection = window.getSelection();
if (selection.rangeCount > 0) {
window.getSelection().deleteFromDocument();
window.getSelection().removeAllRanges();
}
} else if (document.selection) {
// Internet Explorer
var ranges = document.selection.createRangeCollection();
for (var i = 0; i < ranges.length; i++) {
ranges[i].text = "";
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果您只想清除突出显示本身,而不是删除突出显示的文本,以下应该可以解决问题:
function clearSelection() {
if (window.getSelection) {
window.getSelection().removeAllRanges();
} else if (document.selection) {
document.selection.empty();
}
}
Run Code Online (Sandbox Code Playgroud)