javascript:禁用文本选择
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16805684/
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
javascript: Disable Text Select
提问by arpeggio
I'm using javascript to disable text selection on my webiste.
我正在使用 javascript 在我的网站上禁用文本选择。
The code is:
代码是:
<script type="text/JavaScript">
function disableselect(e) {
return false
}
function reEnable() {
return true
}
document.onselectstart = new Function ("return false")
if (window.sidebar) {
document.onmousedown = disableselect
document.onclick = reEnable
}
</script>
Similar script can be found here
类似的脚本可以在这里找到
On my localhost:All Browsers (Firefox, Chrome, IE and Safari) work great.
在我的本地主机上:所有浏览器(Firefox、Chrome、IE 和 Safari)都运行良好。
On my Live site:All ok EXCEPT Firefox.
在我的 Live 网站上:一切正常,除了 Firefox。
My questions are:
我的问题是:
Does anyone have a suggestion as to why Firefox behaves differently for the live site and local host. Note: Javascript is enabled.
Maybe my script is too simplistic so I've tried the followingwith EXACTLY SAME Results
有没有人对为什么 Firefox 对实时站点和本地主机的行为有所不同有任何建议。注意:Javascript 已启用。
也许我的脚本太简单了,所以我用完全相同的结果尝试了以下操作
回答by Zachrip
Just use this css method:
只需使用此 css 方法:
body{
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
You can find the same answer here: How to disable text selection highlighting using CSS?
您可以在这里找到相同的答案:如何使用 CSS 禁用文本选择突出显示?
回答by Yi Feng Xie
I'm writing slider ui control to provide drag feature, this is my way to prevent content from selecting when user is dragging:
我正在编写滑块 ui 控件以提供拖动功能,这是我防止用户拖动时选择内容的方法:
function disableSelect(event) {
event.preventDefault();
}
function startDrag(event) {
window.addEventListener('mouseup', onDragEnd);
window.addEventListener('selectstart', disableSelect);
// ... my other code
}
function onDragEnd() {
window.removeEventListener('mouseup', onDragEnd);
window.removeEventListener('selectstart', disableSelect);
// ... my other code
}
bind startDrag
on your dom:
绑定startDrag
在你的 dom 上:
<button onmousedown="startDrag">...</button>
If you want to statically disable text select on all element, execute the code when elements are loaded:
如果要在所有元素上静态禁用文本选择,请在加载元素时执行代码:
window.addEventListener('selectstart', function(e){ e.preventDefault(); });
回答by Hassan Azimi
For JavaScript use this function:
对于 JavaScript 使用这个函数:
function disableselect(e) {return false}
document.onselectstart = new Function (return false)
document.onmousedown = disableselect
to enable the selection use this:
要启用选择,请使用:
function reEnable() {return true}
and use this function anywhere you want:
并在您想要的任何地方使用此功能:
document.onclick = reEnable
回答by bootsoon
If you got a html page like this:
如果你有一个这样的 html 页面:
<body onbeforecopy = "return false" ondragstart = "return false" onselectstart = "return false" oncontextmenu = "return false" onselect = "document.selection.empty()" oncopy = "document.selection.empty()">
There a simple way to disable all events:
有一种简单的方法可以禁用所有事件:
document.write(document.body.innerHTML)
document.write(document.body.innerHTML)
You got the html content and lost other things.
您获得了 html 内容并丢失了其他内容。
回答by NVRM
One might also use:
还可以使用:
onselectstart = (e) => {e.preventDefault()}
Example:
例子:
onselectstart = (e) => {
e.preventDefault()
console.log("nope!")
}
Select me!
One other alternative, by testing CSS supports, and disable userSelect
, or MozUserSelect
for Firefox.
另一种选择,通过测试 CSS 支持和禁用userSelect
,或MozUserSelect
用于 Firefox。
let FF
if (CSS.supports("( -moz-user-select: none )")){FF = 1} else {FF = 0}
(FF===1) ? document.body.style.MozUserSelect="none" : document.body.style.userSelect="none"
Select me!
回答by Hasan Sheikh
Simple Copy this text and put on the before </body>
简单 复制此文本并放在之前 </body>
function disableselect(e) {
return false
}
function reEnable() {
return true
}
document.onselectstart = new Function ("return false")
if (window.sidebar) {
document.onmousedown = disableselect
document.onclick = reEnable
}