javascript 使 IE 中的东西无法选择
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/4448671/
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
Making things unselectable in IE
提问by Tom Gullen
Here is my chart I've been writing in JS:
这是我用 JS 编写的图表:
The css:
css:
-moz-user-select:none;
-khtml-user-select: none;
Works fine for Chrome/FF, but in IE all the elements are still selectable which looks weird when dragging the bars around.
适用于 Chrome/FF,但在 IE 中,所有元素仍然可以选择,这在拖动栏时看起来很奇怪。
How can I make this unselectable in IE?
如何在 IE 中使其无法选择?
回答by Tim Down
In IE, you need the unselectableattribute in HTML:
在 IE 中,您需要unselectableHTML 中的属性:
<div id="foo" unselectable="on">...</div>
... or set it via JavaScript:
...或通过 JavaScript 设置:
document.getElementById("foo").setAttribute("unselectable", "on");
The thing to be aware of is that the unselectableness is not inherited by children of an unselectable element. This means you either have to put an attribute in the start tag of every element inside the <div>or use JavaScript to do this recursively for an element's descendants:
需要注意的是,不可选择元素不会被不可选择元素的子元素继承。这意味着你要么必须在每个元素的开始标记中放置一个属性,<div>或者使用 JavaScript 为元素的后代递归地执行此操作:
function makeUnselectable(node) {
    if (node.nodeType == 1) {
        node.setAttribute("unselectable", "on");
    }
    var child = node.firstChild;
    while (child) {
        makeUnselectable(child);
        child = child.nextSibling;
    }
}
makeUnselectable(document.getElementById("foo"));
回答by Dennis G
You can use Javascript to make text unselectable in all browsers:
您可以使用 Javascript 使文本在所有浏览器中都无法选择:
document.onselectstart=new Function('return false');
function noselect(e){return false;}
function click(){return true;}
document.onmousedown=noselect;
document.onclick=click;
Another way is described in this SO thread: Is there a way to make text unselectable on an HTML page?
此 SO 线程中描述了另一种方法:是否有一种方法可以使 HTML 页面上的文本无法选择?
The cheapest way probably is <body onselectstart="return false">
最便宜的方法可能是 <body onselectstart="return false">
The best way though is possibly using the following CSS:
最好的方法可能是使用以下 CSS:
[unselectable=on] { -moz-user-select: none; -khtml-user-select: none; user-select: none; }
and add the IE unselectable propertyto the elements you want to make unselectable (unselectable="on"in HTML; element.setAttribute("unselectable","on")in javascript)
并将IE 不可选择属性添加到要使其不可选择的元素(unselectable="on"在 HTML 中;element.setAttribute("unselectable","on")在 javascript 中)
Check out this nice small article about unselectable text.
查看这篇关于不可选择文本的不错的小文章。
回答by Flack
There is an unselectable="on"attribute.
有一个unselectable="on"属性。
http://msdn.microsoft.com/en-us/library/ms537840%28VS.85%29.aspx
http://msdn.microsoft.com/en-us/library/ms537840%28VS.85%29.aspx
And related SO thread: Is there a way to make text unselectable on an HTML page?
和相关的 SO 线程:有没有办法使 HTML 页面上的文本无法选择?
回答by user3396532
This seems to work well in Chrome and IE11 so far, using JQuery...
到目前为止,这似乎在 Chrome 和 IE11 中运行良好,使用 JQuery ......
function fMakeNodeUnselectable(node){      
    $(node).css({"cursor":"default","-moz-user-select":"-moz-none","-khtml-user-select":"none","-webkit-user-select":"none","-o-user-select":"none","user-select":"none"});
    $(node).attr("unselectable","on");
}   

