【JS】.on vs. .click

http://stackoverflow.com/questions/9122078/difference-between-onclick-vs-click

by tea4two

HTML

<button id="add">Add new</button>
    <div id="container">
        <button class="alert">alert!</button>
    </div>

JavaScript

/*
$("button.alert").click(function() {
    alert(1);
});
*/
//ちょっと書き方が違う
var counter = 0;
$('#container button.alert').on('click',function(){
    alert(counter);
}); //won't work
/*動く方
$("#container").on('click', 'button.alert',function() {
    counter++;
    alert(counter);
});*/

$("button#add").click(function() {
    var html = "<button class='alert'>Alert!</button>";
    $("button.alert:last").parent().append(html);
});