jQuery .on() method doesn't see new elements

前端 未结 3 2003
遥遥无期
遥遥无期 2020-12-03 00:50

I\'m getting a JSON element and building a list from its items like this:

getTitles: function(data) {
    data = data || {};
    var list = [];

    $.getJSO         


        
3条回答
  •  感动是毒
    2020-12-03 01:16

    You are not using the correct code to get live functionality.

    $('#title-items').on('click', 'a', function(e) {
        alert('clicked');
        e.preventDefault();
    });
    
    1. First, select your common ancestor element (#title-items in this example). You can use document here too if you want to handle all a elements.
    2. Pass the event type (on), then the sub selector (a), and then the callback function for the event.

    Now, when click events bubble up to #title-items, it will check to see if the element is an a element, and if so, fire the callback.

提交回复
热议问题