Javascript 更新DIV的通用函数

Javascript 更新DIV的通用函数,javascript,php,jquery,Javascript,Php,Jquery,在我的显示页面中,我有一个脚本,可以从另一个php页面更新一个div 如何将以下脚本重新写入: 在我的显示页面(php)中,content是一个php变量,我可以使用updateadiv(divid,content)调用这个函数。(然后脚本将不调用php页面,而是获取输入变量) 函数updatediv(divId,content) (函数($) { $(文档).ready(函数() { $.ajaxSetup( { cache:false, beforeSend:function(){ $(“

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

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


函数updatediv(divId,content)
(函数($)
{
$(文档).ready(函数()
{
$.ajaxSetup(
{
cache:false,
beforeSend:function(){
$(“#内容”).hide();
$(“#加载”).show();
},
完成:函数(){
$(“#加载”).hide();
$(“#内容”).show();
},
成功:函数(){
$(“#加载”).hide();
$(“#内容”).show();
}
});
var$container=$(“#内容”);
$container.load(“http://192.168.1.90/json_output.php");
var refreshId=setInterval(函数()
{
$container.load('http://192.168.1.90/json_output.php');
}, 9000);
});
})(jQuery);
数组());
foreach($devices\u a[devices]作为$value){
$reindexed_devices_a[devices][$value[ref]]]=$value;
}
//echo$reindex_devices_a[devices][59][name];
//在更新到DIV之前需要进行一些条件格式化
如果($reindexed_devices_a[devices][59][name]=='Coffee maker'){
$reindexed_devices_a[devices][59][name]=“覆盖”;
}
回声时间();
//echo$reindex_devices_a[devices][59][name];
}
createarray();
$name59=$reindex_devices_a[devices][59][name];
//检查$name59是否已更改-如果是,则更新div
updatediv('59');
回声“这是一个测试……”;
?>
函数updatediv(DivID,Url){ $(“#加载”).hide(); $('#'+DivID).html(''); $.ajax({ url:url, 成功:函数(rData){ $('#'+DivID).html(rData); } }); }
需要更详细地解释,因为php和javascript在不同的环境中运行,这里没有显示php代码。你的问题目前很难回答understand@charlietfl-谢谢,我已经更新了问题的代码部分。基本上,我想做的是,使用php设计所有逻辑和条件格式,并创建to函数,一个将包含一些设备及其状态的JSON加载到php数组中的函数,用php进行一些格式设置(比如设备XS的值,直到不100%清楚具体问题是什么。如果返回JSON
load()
不会有帮助,因为它是用于html的,需要在收到json后将其解析为html。另外,问题仍然是指
php变量
,没有任何解释。我明白你的意思-我已经尝试现在输入完整的测试代码。我从json加载所有设备,以便测试自上次加载以来发生的更改-我使用php进行测试。如果JSON中的一个值已更改,我对其进行了一些格式化,并将其输出到div供用户查看。我只是不想上传我的梅西代码,以避免太多混淆。必须有一种更好的方法,而不是我所要的-我确定。我不知道你在说什么…
设备
没有上下文是毫无意义的。到目前为止,您只显示了简单的
load()
code
    <!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>
function updateadiv(DivID , Url){ $('#loading').hide(); $('#' + DivID ).html(''); $.ajax({ url:url, success:function(rData){ $('#' + DivID ).html(rData); } }); }