Khác biệt .on và .click trong JQuery
by nguyentienquan
HTML
<!-- https://helpex.vn/question/su-khac-biet-giua-on--click--so-voi-click---5cb1658dae03f6169c9d3c70 -->
<html>
<button id="add">Add new</button>
<div id="container">
<button class="alert">alert!</button>
</div>
</html>
CSS
div#container {
background-color: red;
}
JavaScript
$("button#add").on('click', function() {
var html = "<button class='alert'>Alert!</button>";
$("button.alert:last").parent().append(html);
});
/* $("div#container").click(function() {
alert(1);
}); */
$("div#container").on('click', function() {
alert(1);
});