Running JQuery scripts on AJAX loaded content

前端 未结 4 1957
你的背包
你的背包 2020-12-13 07:45

I am using .load() to pull static HTML files onto my main HTML page. The scripts and selectors that I have written exist within:

$(document).ready(function()         


        
相关标签:
4条回答
  • 2020-12-13 07:55

    jQuery load takes an optional callback function argument which you could use to do any setup you need AFTER your ajax-ed content is loaded

    0 讨论(0)
  • 2020-12-13 08:08

    You can bind events to dynamically loaded content via jQuery's $.live().

    From jQuery http://api.jquery.com/live/:

    Attach a handler to the event for all elements which match the current selector, now and in the future.

    0 讨论(0)
  • 2020-12-13 08:12

    There are more than one option:

    1. you can add initialization scripts [ $(this).click... ] into callback function of $.load()
    2. you can use $.live(), which creates handlers even for dynamically loaded/created objects.

    More here:
    callback: http://api.jquery.com/load/ (notice the "complete()" function)
    bind: http://api.jquery.com/live/

    Edit: My mistake, it was live(), not bind(), thank you guys

    0 讨论(0)
  • 2020-12-13 08:15
    $(document).ajaxComplete(function(){
        // fire when any Ajax requests complete
    })
    

    ajaxComplete()

    0 讨论(0)
提交回复
热议问题