2012-03-24 15 views
0

我正在開發一個web應用程序,並且在此應用程序中,我有時會使用jQuery ajax POST方法加載內容。這工作非常好,但有一個問題。使用jQuery ajax加載的內容POST失去了瞬間的樣式

成功POST後,我加載了我得到的頁面內容(頁面)。 問題是,當它「到達」它沒有風格,我可以看到原始輸出的一秒鐘,然後造型踢。

我怎樣才能避免這個「裸」的時刻?

這是我的代碼:

$.ajax ({ 
    type: "POST", 
    url: "http://example.com/resource/", 
    data: {image_id: image_id}, 
    beforeSend: function (html) { 
     $('#sidebar').html('Loading'); 
    }, 
    success: function (html) { 
     $('#sidebar').html(html); 
    }, 
    error: function (html) { 
     $('#sidebar').html('Something went wrong!'); 
    } 
}); 

回答

2

隱藏它,直到所有數據都到位,然後與內容表現出來:

success: function (html) { 
    $('#sidebar').hide().html(html).show(); 
}, 
+0

我想,但我不幫。 $('#sidebar')。hide(); $('#sidebar')。html(html); $('#sidebar')。delay(1000).show(); – 2012-03-24 23:36:54

+0

@JonathanClark。那麼也許它需要更多的一秒鐘......? – gdoron 2012-03-24 23:38:10

+0

@JonathanClark。你如何獲得它的風格?如果它與CSS應該沒有問題,我的答案是。不需要「延遲」。 – gdoron 2012-03-24 23:47:05