Should all jquery events be bound to $(document)?

后端 未结 4 1058
情深已故
情深已故 2020-11-21 07:48

Where this is coming from

When I first learned jQuery, I normally attached events like this:

$(\'.my-widget a\').click(function() {
    $(this).tog         


        
4条回答
  •  遇见更好的自我
    2020-11-21 08:28

    Apparently, event delegation is actually recommended now. at least for vanilla js.

    https://gomakethings.com/why-event-delegation-is-a-better-way-to-listen-for-events-in-vanilla-js/

    "Web performance # It feels like listening to every click in the document would be bad for performance, but it’s actually more performant than having a bunch of event listeners on individual items."

提交回复
热议问题