对次数变量的计数不是太理解
因为都是n的话,会影响测试二的次数输出,所以我换成了i,但是这个次数的这种计数方式不太明白,触发类型获取的是最后触发的事件类型,就down和up,次数应该是2次,为什么还要++n呢
2016-10-09
直接给你上我的源码吧哈哈
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title></title>
<style>
.left div,
.right div {
width: 100%;
height: 50px;
padding: 5px;
margin: 5px;
float: left;
border: 1px solid #ccc;
}
.left div {
background: #bbffaa;
}
.right div {
background: yellow;
}
</style>
<script src="http://libs.baidu.com/jquery/1.9.1/jquery.js"></script>
</head>
<body>
<h2>删除事件</h2>
<h4>测试一</h4>
<div class="left">
on('mousedown mouseup')
<div class="aaron">点击触发</div>
</div>
<button>点击删除mousedown事件</button>
<script type="text/javascript">
var m = 0,n=0;
//绑定事件
$('.aaron:first').on({
mousedown:function (e) {
++m;
$(this).text( '触发类型:' + (e.type) + ",次数" + m);
},
mouseup:function (e) {
++n;
$(this).text( '触发类型:' + (e.type) + ",次数" + n);
}
});
//删除事件
$("button:first").click(function() {
$(".aaron:first").off('mousedown')
})
</script>
<h4>测试一</h4>
<div class="left">
on('mousedown mouseup')
<div class="aaron">点击触发</div>
</div>
<button>点击销毁所有事件off</button>
<script type="text/javascript">
var i = 0,j=0;
//绑定事件
$('.aaron:last').on({
mousedown:function (e) {
++i;
$(this).text( '触发类型:' + (e.type) + ",次数" + i);
},
mouseup:function (e) {
++j;
$(this).text( '触发类型:' + (e.type) + ",次数" + j);
}
});
//删除事件
$("button:last").click(function() {
$(".aaron:last").off()
})
</script>
</body>
</html>举报