Javascript event listener refresh

前端 未结 2 2020
栀梦
栀梦 2020-12-16 22:28

I have some javascript that I inherited for my job. In this javascript we have a side bar that is constantly updated(every 1 - 10 or so minutes). In the script we parse and

相关标签:
2条回答
  • 2020-12-16 22:58

    You could try this:

    $('body').on('click','.classElem',elm1funct)
             .on('click','.classElem2',elm2funct)
             .on('click','.classElem3', elm3funct);
    

    From jQuery's docs:

    Delegated events have the advantage that they can process events from descendant elements that are added to the document at a later time. By picking an element that is guaranteed to be present at the time the delegated event handler is attached, you can use delegated events to avoid the need to frequently attach and remove event handlers.

    0 讨论(0)
  • 2020-12-16 23:13

    As @crush mentioned, use an event delegated approach to avoid unbinding and re-binding events:

    $(document).on('click', '.classElem', elm1funct);
    
    0 讨论(0)
提交回复
热议问题