3

有一些链接看起来像上面

> <td><a href="http://Lucifase.com/pages/2000.php?refid=2000"
> target="_blank">2000</a><br></td> <td><a
> href="http://Lucifase.com/pages/3000.php?refid=3000"
> target="_blank">3000</a><br></td> <td><a
> href="http://Lucifase.com/pages/4000.php?refid=4000"
> target="_blank">4000</a><br></td> <td><a
> href="http://Lucifase.com/pages/5000.php?refid=5000"
> target="_blank">5000</a><br></td> <td><a
> href="http://Lucifase.com/pages/6000.php?refid=6000"
> target="_blank">6000</a><br></td>

我停在第一步。我无法通过脚本触发器打开它们中的每一个。这是我到目前为止的:

 setTimeout(function() {
    var evt = document.createEvent("MouseEvents");
    evt.initMouseEvent("click", true, true, window,
                       0, 0, 0, 0, 0,
                       false, false, false, false,
                       0, null);
 var links = document.getElementsByTagName('a');
             if(links.href.search('refid') >= 0)

    links.dispatchEvent(evt);
 }, 1000);

但它不起作用,也不知道如何让它们在新标签中一一打开。

4

2 回答 2

3

一一是什么意思?似乎一次“单击”所有链接可以吗?

使用链接时,必须遵循href而不是尝试发送点击事件。以下代码应该只打开您想要的选项卡:

var linksToOpen = document.querySelectorAll ("td > a[href*='refid']");
for (var J = 0, numLinks = linksToOpen.length;  J < numLinks;  ++J) {
    window.open (linksToOpen[J].href, '_blank');
}


OP 澄清更新:
打开每个链接之间有延迟的链接稍微复杂一些。像这样的代码会做到这一点:

var linksToOpen = document.querySelectorAll ("td > a[href*='refid']");

//--- linksToOpen is a NodeList, we want an array of links...
var linksArray  = [];
for (var J = 0, numLinks = linksToOpen.length;  J < numLinks;  ++J) {
    linksArray.push (linksToOpen[J].href);
}

openLinksOnDelay (linksArray);

function openLinksOnDelay (linkArray) {
    //--- Pop the first link off the array...
    var linkToOpen  = linkArray.shift ();
    if (linkToOpen)
        window.open (linkToOpen, '_blank');

    //--- Open the next of the remaining links after a delay.
    if (linkArray.length) {
        setTimeout ( function() {
                openLinksOnDelay (linkArray);
            },
            1000    //--- 1 second.  Use 60000 for 1 minute.
        );
    }
}
于 2012-05-23T21:13:00.337 回答
2

是否需要单击鼠标或可以打开链接:

for(i=0;i<document.links.length;i++) {
  if(document.links[i].target != "_blank"){
     window.open(
       document.links[i].href,
       '_blank'
     );
  }
}
于 2012-05-23T07:21:28.283 回答