2012-06-25 38 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,并且都接受类似的回调来完成。 –