Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 刷新多个iframe_Jquery_Html_Iframe - Fatal编程技术网

Jquery 刷新多个iframe

Jquery 刷新多个iframe,jquery,html,iframe,Jquery,Html,Iframe,我有一个小项目要做,希望使用iframe显示多个页面。所以在我的主HTML页面上,我需要4个iFrame,我需要刷新它们。我只是想让代码更整洁,并以另一种方式访问iFrame,以实现我在这里所做的工作: function refresh() { var iframe = document.getElementById('website1'); iframe.src = iframe.src; setTimeout('refresh()',refreshCy

我有一个小项目要做,希望使用iframe显示多个页面。所以在我的主HTML页面上,我需要4个iFrame,我需要刷新它们。我只是想让代码更整洁,并以另一种方式访问iFrame,以实现我在这里所做的工作:

 function refresh() 
   {
     var iframe = document.getElementById('website1');
     iframe.src = iframe.src;
     setTimeout('refresh()',refreshCycleTime);

     var iframe2 = document.getElementById('website2');
     iframe2.src = iframe2.src;
     setTimeout('refresh()',refreshCycleTime);

     var iframe3 = document.getElementById('website3');
     iframe3.src = iframe3.src;
     setTimeout('refresh()',refreshCycleTime);

     var iframe4 = document.getElementById('website4');
     iframe4.src = iframe4.src;
     setTimeout('refresh()',refreshCycleTime);
   }
我试着让网站1,网站2,网站3。。。所有的getElementById都集成在一个getElementById中,但是考虑到它,getbyID只适用于一个元素。那么,这里还有其他解决方案吗

我曾尝试将一个类应用于iframe而不是id,然后使用getelementsbyclass name方法,但这对我不起作用


谢谢

您可以使用
每个
iframe
选择器:

  function RefreshIframes(){
     $("iframe").each(function() { 
          var src= $(this).attr('src');
         $(this).attr('src',src);  
     });
  }

  setInterval(function(){ 
      RefreshIframes();
  }, 1000);

演示:

这些
iframe.src=iframe.src的确切用途是什么行?如果您想按照问题中的建议使用纯JavaScript(但不在标记中),可以将
document.getElementsByTagName('iframe')[i].src=
在for循环中使用变量
i
进行迭代。感谢您的回复,所以我现在的代码中有这个,但它不起作用。我做错什么了吗?函数refresh(){$('iframe').each(函数(){//$(this).src=$(this).src;//不确定这是否有意义?$(this.contentDocument.location.reload(true);setTimeout('refresh()',refreshCycleTime);});}干杯