清除Firefox中的选择

| 我有这个功能
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!\");
}
}     
现在,在我完成选择之后,我需要清除它。我尝试了几件事没有用,有什么想法吗?
document.selection.clear ()    
这没有用。     
已邀请:
        对于有问题的浏览器:
document.selection.empty()
对于其他浏览器:
window.getSelection().removeAllRanges()
参见http://help.dottoro.com/ljigixkc.php     
        注意:如果要选择输入或textarea元素的文本,则如果您使用输入或textarea的标准本机html元素选择方法,则代码将具有更多的跨浏览器支持。 如果使用本机选择方法选择了html输入或textarea元素,则使用上面建议的方法在我的firefox 44.0.2上不起作用。对于它起作用的,我想对所有浏览器都起作用,正在运行以下代码,该代码创建并选择一个新元素。新元素不能与
display:none
visibility:hidden
一起使用,因为在Firebox中未选择该元素,因此诀窍是通过将所有大小属性​​强制为
0\\none
使其不可见。
var tempElement = document.createElement(\"input\");
tempElement.style.cssText = \"width:0!important;padding:0!important;border:0!important;margin:0!important;outline:none!important;boxShadow:none!important;\";
document.body.appendChild(tempElement);
tempElement.select();
/* Use removeChild instead of remove because remove is less supported */
document.body.removeChild(tempElement);
    
        采用     
tinymce.activeEditor.selection.collapse()
 如果那不起作用,那么使用     
const range = tinymce.activeEditor.dom.createRng();
    tinymce.activeEditor.selection.setRng(range)
    

要回复问题请先登录注册