jquery:从外部html加载值并填充许多本地<div>字段,只读取一次html </div>

时间:2012-12-25 07:07:19

标签: jquery html dynamic external

我现在正在使用此代码,除了向网络服务器发出三个请求外,效果很好:

var refreshspeed=1000

        function moreSnow() {
    $("#uptimedynamic").load("index.html #uptimedynamic");
    $("#cpuloaddynamic").load("index.html #cpuloaddynamic");
    $("#meminfodynamic").load("index.html #meminfodynamic");
    setTimeout("moreSnow()", refreshspeed);
    }

有人可以告诉我如何让它做同样的事情,但只读了一次index.html吗?它需要保持相同的重复循环设置:)

1 个答案:

答案 0 :(得分:0)

这应该这样做

/* cache selectors in main page to avoid searching for them every second*/
var $upTime=$("#uptimedynamic"),  $cpuload = $("#cpuloaddynamic"), $meminfo=$("#meminfodynamic")


function moreSnow() {
    $.get("index.html", function(data){
        /* create a jQuery object from the retrieved page html that can then be traversed*/
        var $data=$(data);

        $upTime.html( $data.find('#uptimedynamic').html() );
        $cpuload.html( $data.find("#cpuloaddynamic").html() );
        $meminfo.html( $data.find("#meminfodynamic").html() );

    });
}

通过仅检索每个元素的内容,您还可以避免重复页面中的ID和潜在的样式问题