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

后端 未结 12 2511
礼貌的吻别
礼貌的吻别 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:53

    NEW ELEMENTS

    As an addendum to the comprehensive answers above to highlight critical points if you want the click to attach to new elements:

    1. elements selected by the first selector eg $("body") must exist at the time the declaration is made otherwise there is nothing to attach to.
    2. you must use the three arguments in the .on() function including the valid selector for your target elements as the second argument.

提交回复
热议问题