Javascript 如何禁用网页上的文本选择

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12315476/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 07:42:35  来源:igfitidea点击:

How to disable selection of text on a web page

javascripthtmlcss

提问by selva_pollachi

I am trying to make webpage very native. How to remove select,select all property in webpage?

我正在尝试使网页非常原生。如何删除选择,选择网页中的所有属性?

回答by Pavlo

Disable selection of every element with CSS

使用 CSS 禁用选择每个元素

body {
  -webkit-user-select: none;
     -moz-user-select: -moz-none;
      -ms-user-select: none;
          user-select: none;
}

This is supported by Chrome, Safari, Firefox, IE 10, and iOS Devices. More info on MDN page.

Chrome、Safari、Firefox、IE 10 和 iOS 设备均支持此功能。有关MDN 页面的更多信息。

Edit:If you want <input>and <textarea>to remain selectable in Firefox, add:

编辑:如果您希望<input><textarea>在 Firefox 中保持可选状态,请添加:

input,
textarea {
     -moz-user-select: text;
}

Disable context menu with jQuery

使用 jQuery 禁用上下文菜单

$(document).on("contextmenu", function (event) { event.preventDefault(); });

回答by Behnam Mohammadi

use this code https://www.docsity.com/it/teorie-e-pratiche-del-web-4/556038/

使用此代码https://www.docsity.com/it/teorie-e-pratiche-del-web-4/556038/

body, html{     
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;  
}

回答by Syracuse Web Design

This JavaScript will Disable select, copy and paste of the content But if user will save page to local machine they will be able to do "anything" they want with your code.

此 JavaScript 将禁用内容的选择、复制和粘贴但是如果用户将页面保存到本地机器,他们将能够使用您的代码做他们想做的“任何事情”。

//disable cut copy past
var message = "";
function clickIE() { if (document.all) { (message); return false; } }
function clickNS(e) {
    if(document.layers || (document.getElementById && !document.all)) {
        if (e.which == 2 || e.which == 3) { (message); return false; }
    }
}
if (document.layers)
{ document.captureEvents(Event.MOUSEDOWN); document.onmousedown = clickNS; }
else { document.onmouseup = clickNS; document.oncontextmenu = clickIE; }
 document.oncontextmenu = new Function("return false")


//for disable select option
document.onselectstart = new Function('return false');
function dMDown(e) { return false; }
function dOClick() { return true; }
document.onmousedown = dMDown;
document.onclick = dOClick;

回答by Manish Prajapati

You Can disable by adding attribute in your body tag oncontextmenu="return false;

您可以通过在您的正文标签中添加属性来禁用 oncontextmenu="return false;

<body oncontextmenu="return false;">

回答by Prashobh

may this help you

这可以帮助你

<div onselectstart="return false;" style="-moz-user-select: none;">

回答by Satya Pendem

element_name{
  -webkit-touch-callout: none; 
  -webkit-user-select: none;
  -khtml-user-select: none; 
  -moz-user-select: none;
  -ms-user-select: none; 
   user-select: none;
 }