简体   繁体   English

从元素中删除特定的jQuery单击事件

[英]Remove specific jQuery click event from element

I understand event can be removed from an element by using .unbind() or .off(). 我理解可以使用.unbind()或.off()从元素中删除事件。 However I am wondering if is that possible to remove specific click event and leave others attached. 但是,我想知道是否可以删除特定的点击事件并留下其他人。 For example assuming I have a element with two events attached as follow: 例如,假设我有一个附加了两个事件的元素,如下所示:

$("#MyDiv").click(function() {
alert("I am first Event");
});

$("#MyDiv").click(function () {
    alert("I am second Event");
});

Is that possible to remove First event and leave second one attached, using some sort of key? 是否有可能删除第一个事件并使用某种键来附加第二个事件?

You can use event name spacing and .off() 您可以使用事件名称间距.off()

$("#MyDiv").on('click.first', function () {
    alert("I am first Event");
});

$("#MyDiv").on('click.second', function () {
    alert("I am second Event");
});

$("#MyDiv").off('click.first');

Demo: Fiddle 演示: 小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM