清除 Firefox 中的选择 [英] Clear a selection in Firefox

查看:15
本文介绍了清除 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!");
}
}     

现在,在我完成我需要对选择执行的操作后,我需要清除它.我尝试了一些没有用的东西,有什么想法吗?

Now after i do what i need to do with the selection i need to clear it. i tried a few things nothing worked, any ideas?

document.selection.clear ()    

这没用.

推荐答案

对于有问题的浏览器:

document.selection.empty()

对于其他浏览器:

window.getSelection().removeAllRanges()

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

这篇关于清除 Firefox 中的选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆