简体   繁体   English

如何在没有输入textarea和选择项目的情况下阻止文本选择

[英]how to block text selection without input textarea and select items

How to block text selection without input textarea and select items i actually have the script that works in IE but not in other browsers, here it is: 如何阻止文本选择没有输入textarea和选择项我实际上有脚本在IE中工作,但在其他浏览器中没有,这里是:

 var omitformtags=["input", "textarea", "select"]
 omitformtags=omitformtags.join("|")
 function disableselect(e){
  if (omitformtags.indexOf(e.target.tagName.toLowerCase())==-1)
   return false
  }
 function reEnable(){
   return true
  }

 if (typeof document.onselectstart!="undefined")
  document.onselectstart=new Function ("return false")
 else{
  document.onmousedown=disableselect
  document.onmouseup=reEnable
 }
  document.onmousedown=click;
  function click()
 {
  if ((event.button==2) || (event.button==3)) { alert("Yazı Kopyalayamazsınız!");}

 }

i need to work it the same way in other browsers, there must be a ready script, just couldnt find in net.. 我需要在其他浏览器中以相同的方式工作,必须有一个准备好的脚本,只是无法在网上找到..

<div onmousedown='return false;' onselectstart='return false;'></div>

onmousedown disables the selection for Firefox, Safari, Opera, Chrome and most other web browsers, onselectstart for IE. onmousedown禁用了对Firefox,Safari,Opera,Chrome和大多数其他网络浏览器的选择,onselectstart for IE。

Though disabling text selection and context menus is very bad style and does not prevent the pro-users from selecting your text. 虽然禁用文本选择和上下文菜单是非常糟糕的样式,但不会阻止专业用户选择您的文本。 Simply disabling JavaScript in their browsers enables the selection again. 只需在浏览器中禁用JavaScript即可再次进行选择。

Updated my answer to be more clear : 更新我的回答更清楚

browsers can disable selecting using css styles or using special element attribute. 浏览器可以禁用使用css样式或使用特殊元素属性进行选择。 This way is better: 这种方式更好:

  • no script to support 没有脚本可以支持
  • no fear that user disables script and copies content she needs 不用担心用户禁用脚本并复制她需要的内容
  • elegant solution 优雅的解决方

Opera and IE ignores such a style, but there is html element attribute unselectable, which denies selecting. Opera和IE忽略了这样的风格,但是html元素属性不可选,否则选择。 Try the following page (I tested in IE8, FF3.6, Safari5, opera11, Chrome8): 尝试以下页面(我在IE8,FF3.6,Safari5,opera11,Chrome8中测试过):

<html>

<head>

<style>

.unselectable {
    -moz-user-select: none; /* for FireFox */
    -webkit-user-select: none; /* for Chrome and Safari */
    -khtml-user-select: none; /* probably old webkit browsers, but new support it too */
    user-select: none; /* for future CSS3 compliant browsers */
}

</style>

</head>

<body>

<!-- add unselectable class to deny selecting in FF, Chrome, Safari and CSS3 compliant browsers -->
<!-- add unselectable attribute to deny selecting in Opera and IE -->
<div class="unselectable" unselectable="on">test</div>

</body>

</html>

For additional information about it visit this page . 有关它的其他信息,请访问此页面 Now you can pick best option for you to use/mantain. 现在您可以选择最适合您使用/ mantain的选项。 One more time - this solution does not need javascript at all and should be imho more safe (if you can edit html\\css pages and don't need dynamic. otherwise you can try to add css class\\element attribute through javascript..) 再一次 - 这个解决方案根本不需要javascript,应该更安全(如果你可以编辑html \\ css页面而不需要动态。否则你可以尝试通过javascript添加css class \\ element属性..)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM