Adding event listeners to current and future elements with a particular class

前端 未结 3 646
挽巷
挽巷 2021-01-01 10:17

With JQuery, is it possible to add an event listener to any element that currently, or will in the future, have a particular class?

I\'m working on

3条回答
  •  耶瑟儿~
    2021-01-01 10:30

    the jQuery live() method swill allow to have a "live" action listener - so if new DOM elements match the selector, they will be attached to the action listener. For example:

    $(".X").live("click", function(){
        alert('some action');
    });
    

    See the documentation here for more info: http://api.jquery.com/live/

    I'm not sure that the second part of your question about keeping the action listener attached after removing the class os possible - someone else might have a solution though.

提交回复
热议问题