jQuery的:从外部HTML负载值和填充许多地方 领域,与HTML的一个读(jquery:

2019-10-17 12:52发布

现在我用这个代码,工作除了它提出了三个请求到Web服务器极大:

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读? 它需要留在相同的重复循环设置:)

Answer 1:

这应该这样做

/* 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() );

    });
}

仅检索每个元素的内容,你alsoo避免dupicating ID在页面和潜在的作风问题



文章来源: jquery: load values from external html and populate many local
fields, with one read of the html