简体   繁体   English

Javascript单击多个链接

[英]Javascript Clicking Multiple Links

I have a webpage page full of links, with check boxes next to them showing that I've clicked them. 我有一个充满链接的网页页面,旁边有复选框,它们表明我已单击它们。 There are hundreds of links, and I would rather not click through all of them. 有数百个链接,我不想单击所有链接。 What I would like to do is write a script that performs a "click" on each of the links, so that I don't have to. 我想要做的是编写一个脚本,该脚本在每个链接上执行“单击”,因此我不必这样做。

var links = document.getElementsByClassName("some-class");

for(var i = 0; i < links.length; i++) {
   links[i].click();
}

My code simply opens the first link and navigates away from the original page, which of course stops execution of the code. 我的代码只是打开第一个链接并导航到原始页面,这当然会停止执行代码。

Can Javascript open links without navigating to them, such as in a new window or tab? Javascript是否可以在不导航到链接的情况下(例如在新窗口或标签中)打开链接? If not, with what language can this be scripted? 如果没有,可以用什么语言编写脚本?

If opening all the links on a new tab solves your problem you should try the code below. 如果在新标签上打开所有链接可以解决您的问题,则应尝试以下代码。

<a href="https://www.link1.com">First Link</a>
<a href="https://www.link2.com">Second Link</a>
<a href="https://www.link3.com">Third Link</a>
<button id="openAll">Open All</button>

<script>
    $("#openAll").click(function(){
        $("a").each(function(){
            window.open($(this).attr("href"), '_blank'); 
        });
    });
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM