如何在没有输入文本区域和选择项目的情况下阻止文本选择
如何在没有输入文本区域和选择项目的情况下阻止文本选择 我实际上有一个可以在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
onmousedown 禁用 Firefox、Safari、Opera、Chrome 和大多数其他 Web 浏览器的选择,onselectstart 禁用 IE。
尽管禁用文本选择和上下文菜单是非常糟糕的风格,并且不会阻止专业用户选择您的文本。只需在浏览器中禁用 JavaScript 即可再次启用该选择。
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.
更新了我的答案,使其更加清晰:
浏览器可以禁用使用 css 样式或使用特殊元素属性的选择。这种方式更好:
Opera 和 IE 忽略这种样式,但有 html 元素属性不可选择,从而拒绝选择。尝试以下页面(我在 IE8、FF3.6、Safari5、opera11、Chrome8 中测试):
有关它的更多信息,请访问
Updated my answer to be more clear:
browsers can disable selecting using css styles or using special element attribute. This way is better:
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):
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..)