Difference between .on('click') vs .click()

后端 未结 12 2479
礼貌的吻别
礼貌的吻别 2020-11-21 05:22

Is there any difference between the following code?

$(\'#whatever\').on(\'click\', function() {
     /* your code here */
});

and

12条回答
  •  遥遥无期
    2020-11-21 05:52

    .click events only work when element gets rendered and are only attached to elements loaded when the DOM is ready.

    .on events are dynamically attached to DOM elements, which is helpful when you want to attach an event to DOM elements that are rendered on ajax request or something else (after the DOM is ready).

提交回复
热议问题