Javascript:每隔10秒从URL数组更改一次location.href?

问题描述 投票:1回答:2

我正在通过gettig href属性的URL获取URL列表,该URL属性是与某个类名匹配的元素数组。直到这里一切都很好。然后,我尝试通过遍历URL数组来每10秒更改页面的location.href。由于某种原因,什么也没发生。我在做什么错?

var buttonsElements = document.getElementsByClassName("elementor-button-link elementor-button");
console.log(buttonsElements.length); 
for(i=0;i<buttonsElements.length;i++) {
    console.log(buttonsElements[i].href); 
    //everything works just fine until here

 setTimeout(function(){
        window.location.href = buttonsElements[i].href;
    },10000);
}
javascript timeout href settimeout location-href
2个回答
1
投票

对我来说,你真的想做什么不是很清楚。注意,一旦分配了href,浏览器将带您进入另一个页面,脚本将被中断。


-1
投票

window.location.href = buttonsElements[i].href;i在您的函数中未引用任何内容,从不递增,等等。您一次使用i遍历了元素,以便console.log()所有元素,但是您并未触及代码中其他任何地方的i。您必须在某处更改其值。

© www.soinside.com 2019 - 2024. All rights reserved.