在jquery中有一个方法可以判断鼠标右键,滑轮,左键被按下。
event.which
起初看一些这方法的说明都是说单击,用了click事件,就试了半天却发现左键可以返回1,滑轮可以返回2,唯独右键一直出现的是菜单栏。后来改用为mousedown和mouseup就可以清楚的判断,鼠标的事件了。
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<script type="text/ecmascript" src="jquery-1.11.3.min.js"></script>
<script type="text/ecmascript" src="JQuery.md5.js"></script>
<style>
*{
margin: 0px auto;
padding:0px;
}
.head{
width:300px;
height:200px;
border:1px solid;
}
.content{
width:50px;
height:50px;
}
</style>
</head>
<body>
<div class="head">
<div class="content">i am here</div>
</div>
<script type="text/javascript">
$(function(){
$(".content").bind("mousedown",function(event){
console.log(event.which);
}).bind("mouseup",function(){
console.log(event.which);
})
//event.target.某属性//获取被触发事件的元素属性
//event.preventDefault();//阻止默认事件的触发,点击提交链接跳
})
</script>
</body>
</html>