gpt4 book ai didi

javascript - 清除 Firefox 中的选择

转载 作者:行者123 更新时间:2023-12-03 13:39:31 24 4
gpt4 key购买 nike

我有这个功能

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com