更新DIV的通用函数

时间:2014-09-28 11:05:12

标签: javascript php jquery

在我的显示页面中,我有一个脚本可以从另一个php页面更新div。

如何将以下脚本重写为: 我可以在我的显示页面(在php中)使用updateadiv(divid,content)调用一个函数,其中内容是一个php变量。 (然后脚本不会调用php页面,而是输入变量)。

    <!DOCTYPE html>
    <html>
    <body>



    function updatediv(divId, content)
    <script src="http://code.jquery.com/jquery-latest.js"></script>
    <script>
    (function($)
    {
        $(document).ready(function()
        {
            $.ajaxSetup(
            {
                cache: false,
                beforeSend: function() {
                    $('#content').hide();
                    $('#loading').show();
                },
                complete: function() {
                    $('#loading').hide();
                    $('#content').show();
                },
                success: function() {
                    $('#loading').hide();
                    $('#content').show();
                }
            });
            var $container = $("#content");
            $container.load("http://192.168.1.90/json_output.php");
            var refreshId = setInterval(function()
            {
                $container.load('http://192.168.1.90/json_output.php');
            }, 9000);
        });
    })(jQuery);
    </script>

    <?php
    function createarray() {
global $reindexed_devices_a ;
$feed_url = "http://192.168.1.90/JSON?request=getstatus&ref=all&location1=all&location2=all";
//$feed_url = "demoxml.xml";

$json = file_get_contents($feed_url);
$devices_a = json_decode($json, true);

//echo $devices_a[Devices][2][name] ;
//echo "<BR> ReIndexed:";

$reindexed_devices_a = array(Devices => array());
foreach ($devices_a[Devices] as $value) {
    $reindexed_devices_a[Devices][$value[ref]] = $value;
}

//echo $reindexed_devices_a[Devices][59][name] ;
//need to do some conditional formatting before updating to DIV's
if ($reindexed_devices_a[Devices][59][name] == 'Coffee maker') {
 $reindexed_devices_a[Devices][59][name] = "overwritten";
}
echo time();

//echo $reindexed_devices_a[Devices][59][name] ;
}

createarray();


$name59=$reindexed_devices_a[Devices][59][name];
//check if $name59 has changed - if yes update div
updatediv('59');

    echo "This is a test <div id = 'name59'>.....</div>";
    ?>



    </body>
    </html>

1 个答案:

答案 0 :(得分:0)

function updateadiv(DivID , Url){


     $('#loading').hide();
      $('#' + DivID ).html('');

      $.ajax({
            url:url,
            success:function(rData){
           $('#' + DivID ).html(rData);
        }

        });
}