声明一下,如果只是用到鼠标的左键和右键,是不用这么复杂的。这里主要是应用到了鼠标左右键同时按下时产生的事件。也可以从代码中自己重新定义网页的鼠标右键菜单。代码如下:
<!DOCTYPE HTML>
<html>
<head>
<title></title>
</head>
<body >
<div oncontextmenu="textmenuClick()" onmouseup="mouse_up()" onmousedown="mouse_down()">
点击测试
</div>
<input type="text" id="mouse_up" value = "" />
<script type="text/javascript">
var clickValue = 0;
var isFirstUP = false;
function mouse_up() {
if (clickValue == 1) {
if (event.button == 1) {
//=======================在这里写左键事件
ElementById("mouse_up").value = "左单键";
//=======================
} else if (event.button == 2) {
//=======================在这里写右键事件
ElementById("mouse_up").value = "右单键";
//=======================
}
clickValue = 0;
} else if (clickValue == 2 && !isFirstUP) {
//=======================在这里写双键事件(且是第二个键弹起时触发)
ElementById("mouse_up").value = "双?键¨¹";
//=======================
clickValue = 0;
}
isFirstUP = false;
}
function mouse_down() {
isFirstUP = true
clickValue ++;
}
function textmenuClick(e) {
//取消网页自带的右键事件
urnValue = false;
event.cancelBubble = true;
}
</script>
</body>
</html>
发布评论