jquery .live on load event

前端 未结 9 1300
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-06 01:06

I need a way to use the jquery .live() function to act on elements that are loaded via ajax.

For instance a div is loaded via ajax .load()<

9条回答
  •  悲哀的现实
    2020-12-06 01:31

    Typically I would handle any actions that occur when the element is loaded either as part of the AJAX callback mechanism or as part of a ready handler that is included in the returned HTML of the AJAX success callback. The later works well when you want to load a partial view onto the page and want to keep the code with the partial view itself.

    Partial View:

    ...

    Using the callback

    $.ajax({
       url: '/example.com/code.php',
       success: function(html) {
              var container = $('#container').html(html);
              $('#myDiv',container).somePlugin( ... );
       }
    });
    

提交回复
热议问题