<!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">點擊觸發(fā)</div>
????</div>
????<button>點擊刪除mousedown事件</button>
????<script?type="text/javascript">
????var?n??=?0;
????//綁定事件
????$(".aaron:first").on('mousedown?mouseup',?function(e)?{
????????$(this).text(?'觸發(fā)類型:'?+??(e.type)?+?",次數(shù)"?+?++n)
????????++n;
????})
????//刪除事件
????$("button:first").click(function()?{
????????$(".aaron:first").off('mousedown')
????})
???
????</script>
????<h4>測試二</h4>
????<div?class="left">
????????on('mousedown?mouseup')
????????<div?class="aaron">點擊觸發(fā)</div>
????</div>
????<button>點擊銷毀所有事件off</button>
????<script?type="text/javascript">
????var?n??=?0;
????//綁定事件
????$(".aaron:last").on('mousedown?mouseup',?function(e)?{
????????$(this).text(?'觸發(fā)類型:'?+??(e.type)?+?",次數(shù)"?+?++n)
????????++n;
????})
????//刪除事件
????$("button:last").click(function()?{
????????$(".aaron:last").off()
????})
???
????</script>
</body>
</html>
2016-11-04
你沒有發(fā)現(xiàn)你沒有刪除之前是加4嗎?
?$(".aaron:first").on('mousedown?mouseup',?function(e)?{
????????$(this).text(?'觸發(fā)類型:'?+??(e.type)?+?",次數(shù)"?+?++n)
????????++n;
????})
這個地方你做了現(xiàn)兩次++n
2016-11-04
一. 原本<測試一>的代碼是這樣的:?
這時運行之后 , 點擊"點擊刪除mousedown事件"的button, 第一次點擊這個button, mousedown次數(shù)為1, mouseup次數(shù)為2, 總次數(shù)為3;第二次以后點擊這個button, mousedown和mouseup的次數(shù)都是加2, 總次數(shù)加4;
二. 看了你的第一次回答, 我將++n;刪除, 代碼為:
這時運行之后 , 點擊"點擊刪除mousedown事件"的button, 點擊這個button, mousedown和mouseup的次數(shù)都是加1了(這和我之前的試驗不太一樣, 不過現(xiàn)在明白了.)
三. 非常感謝, 我明白了!!
2016-11-04
你先去掉一個試試啊,你看東西仔細點好不好,
$("button:first").click(function()?{
????????$(".aaron:first").off('mousedown')
????})
這里只是刪了down沒有刪up??!
我有點不明白你的意思了!