我有一个jquery代码来设置click事件如下: $(“#somediv”)。click(function(){alert('test')});
如何删除上述点击事件?似乎.click()方法总是会附加现有的。
答案 0 :(得分:9)
使用
$('#somediv').unbind('click');
如果您只想删除该功能,则需要对其进行引用:
var test = function() {alert('test');};
$("#somediv").click(test);
window.setTimeout(function () {
$('#somediv').unbind('click', test);
}, 10000);
答案 1 :(得分: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/