2010-07-14 30 views
4

我有一个jQuery代码来设置点击事件如下: $(“#somediv”)。click(function(){alert('test')});如何删除点击事件使用jquery

如何删除上述点击事件?看起来.click()方法总是会附加现有的。

回答

8

使用

$('#somediv').unbind('click'); 

如果你只想要删除的功能,你需要将它的引用:

var test = function() {alert('test');}; 
$("#somediv").click(test); 

window.setTimeout(function() { 
    $('#somediv').unbind('click', test); 
}, 10000); 

http://api.jquery.com/unbind/

+2

您可以通过30秒打我。 – Manfre 2010-07-14 23:24:41

+0

好的..另一个问题,然后..如何删除悬停?它似乎是解除绑定('悬停')不做工作 – user384080 2010-07-15 00:22:06

+2

haha​​hahha ..对不起,从http://stackoverflow.com/questions/805133/how-do-i-unbind-hover-in-jquery找到答案 – user384080 2010-07-15 00:23:43

0
You can use off() method as well. 

on() will be used to create event and off() will be used to remove event. 

function clickEvent() { 
    $("#somediv2").show().fadeOut("slow"); 
}; 


To **remove** events you can use like this, 

$('#somediv').off("click", "#somediv1", clickEvent); 

To **add** events you can use like this, 

$('#somediv').on("click", "#somediv1", clickEvent); 

http://api.jquery.com/off/ 

http://api.jquery.com/on/