unsure about .on() method

后端 未结 2 1084
面向向阳花
面向向阳花 2021-01-13 06:13

I was using the $.click() method for triggering some events. But then I needed to set some events for some HTML elements before the elements were declared. Let\

相关标签:
2条回答
  • 2021-01-13 06:41

    You missed the three-argument version:

    $('body').on('click', 'div.hide', function() { ... });
    

    That puts the handler on the <body> but it catches the events that bubble and invokes the handler when the target element matches the selector (the second argument).

    The handler doesn't have to go on the <body>; it can be any parent container element of your <div>.

    The "on" method replaces the older "live" and "delegate" methods, as well as "bind" itself.

    0 讨论(0)
  • 2021-01-13 06:50

    Use jQuery.live method to attach an event handler for all elements which match the current selector, now and in the future

    0 讨论(0)
提交回复
热议问题