刷新班级的所有div

问题描述

如何根据类名称刷新所有div? 我这样做了,但是不行

setInterval(function()
{

$('.myclass').each(function () {
    $(this.id).load(location.href+" this.id>*","");
});
},10000);

解决方法

$(this.id).load(...在不使用#前缀的情况下创建了无效的选择器,但您真正需要的只是$(this).load(...,因为您已经对该元素进行了引用

然后,您还会在将ID添加到网址末尾时遇到语法错误


您最好只提出一个请求并自己解析响应。这将比对相同资源的大量请求更为有效

setInterval(function(){
   $.get(location.href,function(data){
      // loop through the new elements
      $(data).find('.myclass').each(function(){
          const id = this.id,content = $(this).html();
          // insert into existing element with same id
          $('#' + id).html(content);          
       });
   });

},10000)