javascript 清除 Firefox 中的选择
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6186844/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Clear a selection in Firefox
提问by Ovi
I have this function
我有这个功能
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 ()
this didnt work.
这没有用。
回答by August Karlstrom
For the problematic browser:
对于有问题的浏览器:
document.selection.empty()
For other browsers:
对于其他浏览器:
window.getSelection().removeAllRanges()
回答by Guy L.
Note: in case you are selecting the text of an input or textarea element then your code would have more cross browser support if you would use the standard native html element select method of the input or textarea.
注意:如果您要选择 input 或 textarea 元素的文本,那么如果您使用 input 或 textarea 的标准本机 html 元素选择方法,那么您的代码将具有更多跨浏览器支持。
If an html input or textarea element was selected using the native select method then using the methods suggested above does not work on my firefox 44.0.2. What worked for it, and I suppose works on ALL BROWSERS, is running the following code which creates a new element and selects it. The new element can't be with display:none
or visibility:hidden
because then it is not selected in my Firebox so the trick is to make it invisible by forcing all size attributes to 0\none
.
如果使用本机 select 方法选择了 html input 或 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);
回答by Anand
Use
tinymce.activeEditor.selection.collapse()
if that does not work then use
const range = tinymce.activeEditor.dom.createRng();
tinymce.activeEditor.selection.setRng(range)
tinymce.activeEditor.selection.collapse()
如果那不起作用,请使用
,然后使用
const range = tinymce.activeEditor.dom.createRng();
tinymce.activeEditor.selection.setRng(range)