0

我在 HTML 页面上有以下内容:

<script type="text/javascript">
setInterval(function(){refresh()},5000);
function refresh()
{
    $.post("test.php",
    {
           // nothing here             
    },
    function(data, textStatus)
    {      

      $('' + data + '').find('.maindiv').each(function () {
      $('.maindiv').replaceWith('.maindiv');
      });

    });  
}
</script>

<div id = "1" class= "maindiv">
<div sid= "1" class= "subdiv">old sub div content</div>
<div cid= "1" class= "childdiv">another old sub div</div>
<img id = "1" src="http://foo.com/bar.png"/>  
</div>

<div id = "2" class= "maindiv">
<div sid= "2" class= "subdiv">this content is old</div>
<div cid= "2" class= "childdiv">please update me!</div>
<img id= "2" src="http://foo.com/bar.png"/>  
</div> 

被调用以更新元素的 PHP 页面 (test.php):

<?php

 echo '<div id = "1" class= "maindiv">
       <div sid= "1" class= "subdiv">this is new sub div</div>
       <div cid= "1" class= "childdiv">this is also new sub div content</div>
       <img id= "1" src="http://foo.com/bar.png"/>  
       </div>'; 

 echo '<div id = "2" class= "maindiv">
       <div sid= "2" class= "subdiv">new content</div>
       <div cid= "2" class= "childdiv">new content for this child div</div>
       <img id= "2" src="http://foo.com/bar.png"/>  
       </div>'; 
?>

这两个元素也出现在 HTML 页面上。基本上我想调用 PHP 页面并从 PHP 页面获取这些元素并在 HTML 页面的响应中循环遍历它们,并将每个 maindiv(包括其所有子元素)替换为 HTML 页面上的相应元素。

这只是一个非常基本的例子。我这样做是因为这些元素中的内容发生了变化,并且想要替换像实时更新这样的元素。

我发布的功能似乎没有响应,所以不知道如何实现这一点。

提前致谢

4

2 回答 2

1

You need to index the maindiv elements in page so that they match with the new elements.

Also need to modify what you are replacing with

 $(data ).filter('.maindiv').each(function (index) {/* filter or find depends on html structure sent*/
      /* "this" is the current div in response*/          
      $('#'+this.id).replaceWith(this);
});

each will track index, eq() is used to match same index in page.

EDIT: Also change find() to filter() if only output is the DIV's in root of output

于 2013-02-08T02:22:57.593 回答
0

Why dont you have 1 div that surrounds the content that you want to poll then upon receiving the ajax response replace the whole block with html()

<?php 
//check for ajax and return
if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest'){
    header('Content-Type: text/html');

    $response = '
    <div id = "a" class= "maindiv">
       <div sid= "1" class= "subdiv">'.date("D M j G:i:s T Y").'</div>
       <div cid= "1" class= "childdiv">'.md5(time()).'</div>
       <img id= "1" src="http://foo.com/bar.png"/>  
    </div>
    <div id = "a" class= "maindiv">
       <div sid= "1" class= "subdiv">'.date("D M j G:i:s T Y").'</div>
       <div cid= "1" class= "childdiv">'.md5(time()+1).'</div>
       <img id= "1" src="http://foo.com/bar.png"/>  
    </div>'; 

    echo $response;
    die;
}
?>

<script type="text/javascript">
function poll(){
    setTimeout(function(){
        $.ajax({ url: "./test.php", cache: false,
        success: function(data){
            $("#polling").html( data );
            poll();
        }});
    }, 5000);
}
$(document).ready(function(){
    poll();
});
</script>


<div id = "polling" class= "maindiv">
    <!--New response will replace the below html-->
    <div id = "1" class= "maindiv">
    <div sid= "1" class= "subdiv">old sub div content</div>
    <div cid= "1" class= "childdiv">another old sub div</div>
    <img id = "1" src="http://foo.com/bar.png"/>  
    </div>

    <div id = "2" class= "maindiv">
    <div sid= "2" class= "subdiv">this content is old</div>
    <div cid= "2" class= "childdiv">please update me!</div>
    <img id= "2" src="http://foo.com/bar.png"/>  
    </div> 

</div>

Edit

You can also return json and then loop the response into the divs based on id Here is an example:

<?php 
//check for ajax and return
if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest'){
    header('Content-Type: application/json');

    $response = array(
    'block_a'=>'content for block a (last_updated:)'.date("D M j G:i:s T Y"),
    'block_b'=>'content for block b (last_updated:)'.date("D M j G:i:s T Y"),
    'block_c'=>'content for block c (last_updated:)'.date("D M j G:i:s T Y"),
    'block_d'=>'content for block d (last_updated:)'.date("D M j G:i:s T Y"),
    'block_e'=>'content for block e (last_updated:)'.date("D M j G:i:s T Y"),
    ); 

    echo json_encode($response);
    die;
}
?>


<script type="text/javascript">
function poll(){
    setTimeout(function(){
        $.ajax({ url: "./test.php", cache: false,
        success: function(data){
            $.each(data, function(k, v) {
                //key value place html blocks
                $("#"+k).html(v);
            });
            poll();
        }, dataType: "json"});
    }, 1000);
}

$(document).ready(function(){
    poll();
});
</script>


<div id = "block_a" class= "maindiv"></div>
<div id = "block_b" class= "maindiv"></div>
<div id = "block_c" class= "maindiv"></div>
<div id = "block_d" class= "maindiv"></div>
<div id = "block_e" class= "maindiv"></div>

This way you can build $response with only the content you want replaced upon each poll.

于 2013-02-08T02:36:04.397 回答