最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - What is the difference between .click(...) and .live('click', ...)? - Stack Overflow

matteradmin8PV0评论

Consider the following code:

HTML:

<div id='button' class='enabled'>Press here</div>
<div id='log'></div>

CSS:

#button {
    width: 65px;
    height: 25px;
    background-color: #555;
    color: red;
    padding: 10px 20px;
}
#button.enabled {
    color: #333;
}
#button.enabled:hover {
    color: #FFF;
    cursor: pointer;
}

JavaScript:

$(function() {
    $('#button.enabled').live('click', function() {    // (1)
    //$('#button.enabled').click(function() {          // (2)
        log('#button.enabled clicked');
    });
});
function log(str) {
    $('#log').append(str + '<br />');
    $('#button').toggleClass('enabled');
}

This code works as expected, i.e. log() is called only when enabled button is clicked.

But, if I replace (1) with (2), log() is called also when not enabled button is clicked.
Why is that ?
What is the difference between (1) and (2) ?

Consider the following code:

HTML:

<div id='button' class='enabled'>Press here</div>
<div id='log'></div>

CSS:

#button {
    width: 65px;
    height: 25px;
    background-color: #555;
    color: red;
    padding: 10px 20px;
}
#button.enabled {
    color: #333;
}
#button.enabled:hover {
    color: #FFF;
    cursor: pointer;
}

JavaScript:

$(function() {
    $('#button.enabled').live('click', function() {    // (1)
    //$('#button.enabled').click(function() {          // (2)
        log('#button.enabled clicked');
    });
});
function log(str) {
    $('#log').append(str + '<br />');
    $('#button').toggleClass('enabled');
}

This code works as expected, i.e. log() is called only when enabled button is clicked.

But, if I replace (1) with (2), log() is called also when not enabled button is clicked.
Why is that ?
What is the difference between (1) and (2) ?

Share Improve this question edited Sep 6, 2010 at 13:38 Nick Craver 631k138 gold badges1.3k silver badges1.2k bronze badges asked Sep 6, 2010 at 12:00 Misha MoroshkoMisha Moroshko 171k230 gold badges520 silver badges760 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 14

The difference is that .click() binds a click handler to the element. That's the most important thing, to the element, so whatever elements the $('#button.enabled') selector matches at the time it's bound, get bound...regardless of it the selector no longer matches later.

.live() checks the selector at the time of the event to see if it should run the handler...so changing the class does matter, since it no longer matches. The .live() handler lives on document and relies on event bubbling, so it must check the selector to see if it came from an element that it should execute the handler for.

In number 2 the click function is applied to all enabled buttons at that moment. So if a button is not enabled when the function is called it will never be enabled.

In number 1 the click function is applied when-ever needed -- that is if there is a change to the DOM that element is checked again to see if it need to have the click function applied to it.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far