jquery ajax到temp div然后在div上加载div

时间:2012-06-25 18:59:10

标签: jquery

好的,我有一个ajax jsonp请求,它抓取一大块html,然后将它扔到页面上的div中。麻烦是,随着数据加载,内容类型的junks到位(因为它在div中构建html)

所以我想先将它加载到一个临时的off page 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

    });

2 个答案:

答案 0 :(得分: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>

这也假设您正在从后端收到/发送回复。同样,这是一个假设的答案,但我希望它能引导你朝着正确的方向前进。

答案 1 :(得分: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,你可以使用回调函数将你的缓冲div中的html复制到你的屏幕上。