admin管理员组

文章数量:1296301

I understand event can be removed from an element by using .unbind() or .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?

I understand event can be removed from an element by using .unbind() or .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?

Share Improve this question asked Jan 27, 2015 at 9:27 cyrus-dcyrus-d 8431 gold badge14 silver badges36 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 13

You can use event name spacing and .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

本文标签: javascriptRemove specific jQuery click event from elementStack Overflow