2012-06-25 49 views
0

好吧,我有一個ajax jsonp請求,抓取一個html塊,然後扔在頁面上的div。麻煩的是,隨着數據的加載,垃圾的內容種類到位(因爲它在div內建立html)jquery ajax到臨時div然後加載div

所以我想先加載它到一個臨時離頁div,一旦完成加載,移動內容到我的主要分區。

$.getJSON('getsomedata.php?jsoncallback=?', 

    function(data) { 

    if(data.success=="true") 

    { 

    // load into temp div 


    // pause? until loaded? 


    // move content into the real div 

    }); 

回答

0

,如果我理解正確的話,你可以在你的代碼中使用類似如下

//Will load a message into a temporary div 
$('#temp-div').html("Temporary loading message"); 

//Will replace loading message with the server response 
$('#temp-div').html(response); 

的,你有東西(HTML)

<div id="temp-div"> </div> 

這種效果也會假設你從後臺獲取/發送響應。再次,這是一個假設的答案,但我希望它能引導你朝着正確的方向前進。

+0

問題是我想從屏幕上構建div,所以用戶沒有看到加載到div的東西,然後加載後,移動到視圖。有沒有辦法在繼續之前等待div的所有內容完成加載? –

+0

您可以使用諸如「

」之類的東西來「隱藏」div。因此,不要使用'$('#temp-div')。html(「Temporary loading message」);'代碼,你可以用'$('#temp-div')。show()替換那行。 ' – Adam

0

您可以使用回調函數在ajax請求完成時顯示div。

$.post('location_of_ajax_file', 
    { 'post vals': 'with values'}, 
    function(data) { 
     // this ambiguous function can also just be a callback reference 
     // to an external function. 
     // It gets called when the ajax request is complete, so you can load 
     // everything into a hidden div, then use this function to show 
     // the div like so: 
     $('#hiddendiv').fadeIn(200); 

     // fadeIn is cool, but you could also just set the css atribute to show, 
     // or switch to another class that includes a show expression. 
    } 
); 

如果你真的想要切換的div,你可以使用回調函數的HTML從你的緩衝DIV複製到屏幕上的一個爲好。

+0

.post與其他ajax函數的工作方式相同,順便說一句,我只是用它作爲示例。它們都只是簡單的函數,無論如何調用.ajax,並且都接受類似的回調來完成。 –