javascript 实现禁止右键,复制,选取文本 (兼容firefox,IE,chrome等主流浏览器)
1. JS 禁止右键
Html代码 收藏代码
程序代码: | [ 复制代码到剪贴板 ] |
<script type="text/javascript">document.oncontextmenu=function(e){return false;}</script>
<body onselectstart="return false">
......
<body onselectstart="return false">
......
2. CSS 禁止复制和选取
如果让整个页面都禁止选择
Html代码 收藏代码
程序代码: | [ 复制代码到剪贴板 ] |
<style type="text/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;
}
</style>
body {
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
</style>
如果是局部
Html代码 收藏代码
程序代码: | [ 复制代码到剪贴板 ] |
<style type="text/css">
.unselectable {
-moz-user-select: -moz-none;
-khtml-user-select: none;
-webkit-user-select: none;
/*
Introduced in IE 10.
See http://ie.microsoft.com/testdrive/HTML5/msUserSelect/
*/
-ms-user-select: none;
user-select: none;
}
</style>
.unselectable {
-moz-user-select: -moz-none;
-khtml-user-select: none;
-webkit-user-select: none;
/*
Introduced in IE 10.
See http://ie.microsoft.com/testdrive/HTML5/msUserSelect/
*/
-ms-user-select: none;
user-select: none;
}
</style>
3. 完整实例:
Java代码 收藏代码
程序代码: | [ 复制代码到剪贴板 ] |
<style type="text/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;
}
</style>
<script langauge="javascript">
document.oncontextmenu=function(e){return false;}
</script>
</head>
<body onselectstart="return false">
... ...
body {
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
</style>
<script langauge="javascript">
document.oncontextmenu=function(e){return false;}
</script>
</head>
<body onselectstart="return false">
... ...
参考: http://outofmemory.cn/code-snippet/310/css-dis ... ext-jianrong-suoyou-liulanqi
或者:
Html代码 收藏代码
程序代码: | [ 复制代码到剪贴板 ] |
body{
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
function iEsc(){ return false; }
function iRec(){ return true; }
function DisableKeys() {
if(event.ctrlKey || event.shiftKey || event.altKey) {
window.event.returnValue=false;
iEsc();}
}
document.ondragstart=iEsc;
document.onkeydown=DisableKeys;
document.oncontextmenu=iEsc;
if (typeof document.onselectstart !="undefined") document.onselectstart=iEsc;
else
{
document.onmousedown=iEsc;
document.onmouseup=iRec;
}
function DisableRightClick(e)
{
if (window.Event){ if (e.which == 2 || e.which == 3) iEsc();}
else
if (event.button == 2 || event.button == 3)
{
event.cancelBubble = true
event.returnValue = false;
iEsc();
}
}
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
function iEsc(){ return false; }
function iRec(){ return true; }
function DisableKeys() {
if(event.ctrlKey || event.shiftKey || event.altKey) {
window.event.returnValue=false;
iEsc();}
}
document.ondragstart=iEsc;
document.onkeydown=DisableKeys;
document.oncontextmenu=iEsc;
if (typeof document.onselectstart !="undefined") document.onselectstart=iEsc;
else
{
document.onmousedown=iEsc;
document.onmouseup=iRec;
}
function DisableRightClick(e)
{
if (window.Event){ if (e.which == 2 || e.which == 3) iEsc();}
else
if (event.button == 2 || event.button == 3)
{
event.cancelBubble = true
event.returnValue = false;
iEsc();
}
}
|
暂时没有评论
发表评论 - 不要忘了输入验证码哦! |