30秒后会重新加载几次iframe

时间:2019-03-13 17:46:46

标签: javascript iframe

我有一个包含多个iframe的页面,我需要一个脚本,该脚本会在30秒后重新加载iframe。到目前为止,我只发现仅适用于iframe的脚本,而其他脚本则不刷新。有没有办法同时重新加载几个?

编辑:无需超时

$(function() {
    setInterval(refreshiframe, 5000);
});

function refreshiframe() {
    $('#testframe').attr('src', $('#testframe').attr('src'));
}

2 个答案:

答案 0 :(得分:0)

这真的很简单。

function refreshiFrames(selectors){
    //selectors is an array, it could be --  var selectors = ["#abc",".xyz"];  to get all the iframes that have Id "abc" or class "xyz"

    for(let i =0; i<selectors.length; i++){
        let iframes = document.querySelectorAll(selectors[i]);
        for (let j =0; j<iframes.length; j++){
             iframes[j].src = iframes[j].src;
        }
    }
}

答案 1 :(得分:-1)

重新加载所有iframes

let iFrames = document. getElementsByTagName('iframe');

for (let i = 0; i < iFrames.length; i++){
  iFrames[i].src = iFrames[i].src;
}

按类别重新加载几个iframes

let iFrames = document. getElementsByClassName('element-class');

for (let i = 0; i < iFrames.length; i++){
  iFrames[i].src = iFrames[i].src;
}

通过ID重新加载一个iframe

let iFrame = document.getElementById('element-id');
iFrame.src = iFrame.src;