使用 JavaScript 遍历 href

Loop through href using JavaScript

我有一个数组 geneList,其中应用了循环。因此,href 将随着循环迭代而改变,其中 hangoutButton 将自动单击并重定向到给定的 url。听起来很简单

问题:- 循环正在迭代您可以在控制台中看到您正在迭代但 href 值没有迭代。它像 href 一样只停留在第一个索引上。当页面重定向时,您将在每次迭代中看到第一个索引到键的数组。您可以观察到密钥在控制台中迭代。

谁能帮忙

提前致谢.......

var geneList= ["123", "456", "678","2345"];
   
geneList.forEach(function(key){
                // GSEA Redirect Button
                var sample= document.createElement('a');
                sample.href = "forEdit3.php?randNum=" + key;
                sample.target = "_blank";
                sample.style.display = "none";
                var samplebutton = document.createElement('button');
                samplebutton .innerHTML = "BarPlot";
                samplebutton .className = "btn";
                samplebutton .id = "autoClickbarPlot";
                forPlot.appendChild(sample);
                sample.appendChild(samplebutton );
                var hangoutButton = document.getElementById("autoClickbarPlot");
                hangoutButton.click();
                console.log(key);
            });

点击第一个元素的原因是您对多个元素使用相同的 ID,因此当您设置 hangoutButton 时,将始终 return 第一个项目,因为您正在尝试 select 由 ID.

要使其正常工作,请将 key 值连接到元素 ID。

像这样:

sampleButton.id = "autoClickbarPlot_" + key;

然后

var hangoutButton = document.getElementById("autoClickbarPlot_" + key);

这应该足以 select 您想要的 hangoutButton