ammayjxf 发表于 2013-2-7 14:36:45

onContextmenu

用oncontextmenu事件单禁用右键菜单

onconTextmenu=window.event.returnValue=false;右键菜单禁用,用这个可以禁止复制。



在<body>中加入属性代码:

oncontextmenu="return false"               

onselectstart="return false"                   禁止选中网页上的内容

oncopy="return false"                           防复制用户在网页上选中的内容



防止用户另存网页:

利用<noscript><iframe src=*.html></iframe></noscript>标签,能防止网页的直接另存,但不能防止网页被人使用工具下载

*为通配符。

例1:

<html>
<head>
<title>OnContextMenu事件</title>

<script language="JavaScript">
<!--

function uFunction()

{   document.all.infoDiv.innerHTML='你按下了鼠标右键,但是右键菜单不能 显示!';}

function uFunction2()

{    document.all.infoDiv.innerHTML='你按下了Ctrl+鼠标右键,可以 显示右键菜单。';}

//-->

</script>
</head>

<body oncontextmenu="if(!event.ctrlKey){uFunction();return false}else{uFunction2()}">

<div id="infoDiv">你按下了鼠标右键,但是右键菜单不能 显示!<br>你按下了Ctrl+鼠标右键,可以显示右键菜单。
</div></body>
</html>



例2:

view plaincopy to clipboardprint?
<SPAN class=t><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"></SPAN>
<html><head>
    <title>利用OnMousedown和OnContextmenu为表格添加鼠标左中右键单击的处理</title>
    <script type="text/javascript">
//   
var keyArray = new Array(   
    new Array(0, "右键"),   
    new Array(1, "左键"),   
    new Array(2, "右键"), // 测试在IE7中按右键是2,在Maxthon2.0正式版中是0   
    new Array(3, "左键右键同时按"),//在IE7中我测试捕获不到,慎用   
    new Array(4, "中键")   
    //测试同时按两个键更多的表示   
    //new Array(6, "中键右键同时按")   
);   
function Click()   
{   
    var message = GetKeyMessage(event.button);   
    alert(message);   
    if (event.button == 2 || event.button == 0) //按右键,// 测试在IE7中按右键是2,在Maxthon2.0正式版中是0   
    {   
      //处理代码   
    }   
}   
function GetKeyMessage(button)   
{   
    for (var i = 0; i < keyArray.length; i++)   
    {   
      if (keyArray == button)   
      {   
            return keyArray + ", event.button = " + button;   
      }   
    }   
    return "未知组合键, event.button = " + button;   
}   
</script>
</head><body>
<table cellpadding="0" cellspacing="0" border="1">
<tr>
<!--oncontextmenu="return false"屏蔽快捷菜单-->
    <td oncontextmenu="return false" onmousedown="Click()">请分别用左键、右键、中键、左键右键组合点这里测试</td>
</tr>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head>
    <title>利用OnMousedown和OnContextmenu为表格添加鼠标左中右键单击的处理</title>
    <script type="text/javascript">
//
var keyArray = new Array(
    new Array(0, "右键"),
    new Array(1, "左键"),
    new Array(2, "右键"), // 测试在IE7中按右键是2,在Maxthon2.0正式版中是0
    new Array(3, "左键右键同时按"),//在IE7中我测试捕获不到,慎用
    new Array(4, "中键")
    //测试同时按两个键更多的表示
    //new Array(6, "中键右键同时按")
);
function Click()
{
    var message = GetKeyMessage(event.button);
    alert(message);
    if (event.button == 2 || event.button == 0) //按右键,// 测试在IE7中按右键是2,在Maxthon2.0正式版中是0
    {
      //处理代码
    }
}
function GetKeyMessage(button)
{
    for (var i = 0; i < keyArray.length; i++)
    {
      if (keyArray == button)
      {
            return keyArray + ", event.button = " + button;
      }
    }
    return "未知组合键, event.button = " + button;
}
</script>
</head><body>
<table cellpadding="0" cellspacing="0" border="1">
<tr>
<!--oncontextmenu="return false"屏蔽快捷菜单-->
    <td oncontextmenu="return false" onmousedown="Click()">请分别用左键、右键、中键、左键右键组合点这里测试</td>
</tr>view plaincopy to clipboardprint?
<tr>
    <td>这个表格没有处理,点这里没反应</td>
</tr>
</table>
</body>

<tr>
    <td>这个表格没有处理,点这里没反应</td>
</tr>
</table>
</body>view plaincopy to clipboardprint?
</html>
页: [1]
查看完整版本: onContextmenu