2013-01-09 51 views
1

如何向調用jquery.load()的div顯示「loading ...」文本。顯示「正在加載...」文本 - 文檔中所有div只有一個jquey.ajaxSend()

$(document).ready(function() 
{ 
    //only one ajaxSend for all divs 
    $(document).ajaxSend(function() 
    { 
     $(this).html('LOADING...'); //'this' doesn't works 
    }); 

    //... 

    $("#ranking-content").load("content/ranking.php"); 
    $("#empleados-content").load("content/empleados.php"); 
    //... more and more 

}); 

在ajaxSend回調裏面,我怎樣才能得到發出ajax請求的div的ID?

我敢肯定,有一個更好的辦法比做一個ajaxSend每格。

回答

3

要定製特定DIV的行爲,使用ajax功能:

定義beforeSend(腳本Ajax調用之前運行)和context(HTML元素):

$.ajax({ 
    context: $('#ranking-content'), 
    url: 'content/ranking.php', 
    beforeSend: function (xhr) { 
    $(this).html('Loading'); 
    } 
}).done(function (data) { 
    $(this).html(data); 
}); 

使用此不需要ajaxSend。您可以通過將參數與調用者提供的設置合併到ajax來重新使用此代碼。

相關問題