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

发布于 2024-10-12 00:30:26 字数 708 浏览 2 评论 0原文

如何在没有输入文本区域和选择项目的情况下阻止文本选择 我实际上有一个可以在 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!");}

 }

我需要在其他浏览器中以同样的方式工作,必须有一个现成的脚本,只是在网络中找不到..

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:

 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..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

晌融 2024-10-19 00:30:26
<div onmousedown='return false;' onselectstart='return false;'></div>

onmousedown 禁用 Firefox、Safari、Opera、Chrome 和大多数其他 Web 浏览器的选择,onselectstart 禁用 IE。

尽管禁用文本选择和上下文菜单是非常糟糕的风格,并且不会阻止专业用户选择您的文本。只需在浏览器中禁用 JavaScript 即可再次启用该选择。

<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.

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.

你怎么这么可爱啊 2024-10-19 00:30:26

更新了我的答案,使其更加清晰

浏览器可以禁用使用 css 样式或使用特殊元素属性的选择。这种方式更好:

  • 没有脚本支持,
  • 不用担心用户禁用脚本并复制内容,她需要
  • 优雅的解决方案

Opera 和 IE 忽略这种样式,但有 html 元素属性不可选择,从而拒绝选择。尝试以下页面(我在 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>

有关它的更多信息,请访问

Updated my answer to be more clear:

browsers can disable selecting using css styles or using special element attribute. 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. Try the following page (I tested in 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. 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..)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文