开发者

how to remove the click event using jquery

I have a jquery code to set the click event as follow: $("#somediv").click(function() {alert('test')});

How do I remove the above click event? it seems that the .click() method will 开发者_开发百科always append the exisiting ones.


Use

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

If you only want to remove that function, you need a reference to it:

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

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

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


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/
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜