jQuery - How do I bind events to hidden elements that will be shown later?

后端 未结 6 1170
花落未央
花落未央 2020-12-08 00:15

I am trying to attach \'click\' events to all elements of a particular class. The problem is some of the elements are on a tab that is hidden (display: none) at the time tha

6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-08 00:43

    As of jQuery 1.7, the .live() method is deprecated.

    You can now use the .on() method. By attaching a delegated event to an element that is viewable in the HTML when the jQuery is run.

    So if a.someClass is hidden when jQuery is run, you should attach a delegated event to the body, so it will run when there is a viewable a.someClass element in the future, for example:

    $('body').on('click','input.a.someClass',function(){
      alert("test");
    });
    

提交回复
热议问题