2011-12-21 62 views
0

我有一個div類「item」。當我在div上使用.removeClass('item')時,它不會停止它的jquery功能。這裏是我的代碼:從div中刪除類不會停止jquery函數

<div class = "item"> 
    //stuff in here 
</div> 

而jQuery的:

$(".item").mouseenter(function() { 
    $(this).find(".clear").show(); 
}); 
$(".clear").click(function() { 
    $(this).closest("div").removeClass('item'); 
}); 

當我刪除它的類(和我成功地刪除它,我有一些CSS測試它),它仍然響應jQuery代碼。我怎樣才能使它不會響應該代碼?

回答

3

您需要unbind()事件處理程序。

使用

$(this).closest("div").removeClass('item').unbind('mouseenter'); 

備選地可以綁定到一個容器元件的初始事件(document的或)和委託給它的處理。這樣,一旦類被刪除,它確實不會對點擊做出反應。

使用1.7方法

$(document).on('mouseenter','.item',function() { 
    $(this).find(".clear").show(); 
}); 

使用1.6及以下方法

$(document).delegate('.item','mouseenter', function() { 
    $(this).find(".clear").show(); 
}); 
+0

冷卻它的工作原理謝謝了! – user1108749 2011-12-21 01:52:55