在Firefox中清除选择

Ovi*_*Ovi 17 javascript range selection clear

我有这个功能

function smth() {
var container = null;
var newContainer = null;
if (window.getSelection) {  // all browsers, except IE before version 9
    alert("first if");
    var selectionRange = window.getSelection();
    if (selectionRange.rangeCount > 0) {
        var range = selectionRange.getRangeAt(0);
        container = range.commonAncestorContainer;
        newContainer = container;
    }
}
else {
    if (document.selection) {   // Internet Explorer
        alert("second if");
        var textRange = document.selection.createRange();
        container = textRange.parentElement();
    }
}

if (newContainer) {
    return newContainer.nodeName;
}
else {
    alert("Container object for the selection is not available!");
}
}     
Run Code Online (Sandbox Code Playgroud)

现在,在我做了我需要做的选择后,我需要清除它.我尝试了一些没有用的东西,任何想法?

document.selection.clear ()    
Run Code Online (Sandbox Code Playgroud)

这没用.

Aug*_*rom 62

对于有问题的浏览器:

document.selection.empty()
Run Code Online (Sandbox Code Playgroud)

对于其他浏览器:

window.getSelection().removeAllRanges()
Run Code Online (Sandbox Code Playgroud)

http://help.dottoro.com/ljigixkc.php

  • 不再是“针对有问题的浏览器”。这里的后一个选项适用于所有浏览器,包括IE的最新版本。(假设您用“问题”来表示IE。) (3认同)