我有这个功能
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
关于javascript - 清除 Firefox 中的选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6186844/