我最近遇到了website that disabled text selection,阻止任何人轻松地复制和粘贴文本。我有一个小书签,禁止使用JavaScript阻止上下文菜单的类似尝试,我想知道是否可以为文本选择做类似的事情。使用JavaScript启用阻止的文本选择
function disableSelection(target){
if (typeof target.onselectstart!="undefined") //For IE
target.onselectstart=function(){return false}
else if (typeof target.style.MozUserSelect!="undefined") //For Firefox
target.style.MozUserSelect="none"
else //All other route (For Opera)
target.onmousedown=function(){return false}
target.style.cursor = "default"
}
其他地方的函数调用disableSelection(document.body)
。
从我的上下文菜单中选择书签的解决方案也可能是必要的:
javascript:void(document.onmousedown=null);
void(document.onclick=null);
void(document.oncontextmenu=null)
最后,我看到了elsewhere on StackOverflow是CSS也可用于:
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-o-user-select: none;
user-select: none;
有没有打所有的方法这些立即结束这个暴虐通过我的浏览器?我如何为所有元素启用MozUserSelect
/SelectStart
并设置CSS属性?
这是一个非常快速downvote ...任何解释? – Patrick