之前做項(xiàng)目遇到同一個dom上要同時綁定單擊和mousedown事件瞳氓, 然后就發(fā)現(xiàn)沖突了,總是先執(zhí)行mousedown事件栓袖,click時間不會被執(zhí)行匣摘。因?yàn)橄胍猚lick的時候必然先mousedown,所以……
后來通過setTimeout解決了此沖突裹刮,簡單總結(jié)demo如下:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<style type="text/css">
div{
width:100px;
height:100px;
background:green;
}
</style>
<body>
<div class="demo"></div>
</body>
</html>
<script src="http://cdn.static.runoob.com/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var down=null;
var mouse=false;
$('.demo').mousedown(function(){
clearTimeout(down);
down=setTimeout(function(){
mouse=true;
console.log('鼠標(biāo)按下了');
},200);
});
$('.demo').click(function(){
clearTimeout(down);//要清除down事件音榜,否則在執(zhí)行了click事件之后,會接著執(zhí)行mousedown事件
if(!mouse){
console.log('被點(diǎn)擊了');
}else{
mouse=false;
}
});
});
</script>