在多个浏览器Windows /选项卡中打开链接

我需要单击多个链接(选择它们),然后单击一个按钮,打开新窗口或选项卡中的所有选定链接。 这当然取决于浏览器的行为。

我的计划是使用Javascript将选定的链接添加到数组,然后在单击提交按钮时,javascript将运行数组并为每个项目打开一个新窗口。 我可能会在jQuery中这样做。

有没有人做过类似的事情? 还有替代品吗?

我想你是对的。

实现你所描述的恕我直言的最好方法是将你想要在新窗口中打开的链接的URL放入数组中,使用return false; 为了防止实际打开链接然后使用某种循环打开所有这些。

我冒昧地将几行jQuery代码放在一起,这些代码将完成您所描述的内容:

 $(document).ready(function() { var $hash = new Array(); // We create new Array $('a').click( function(){ // On each click to  element if ( $(this).attr("data-pack") == "true" ) { // check wether this is one of the links we use $hash[$(this).attr("id")] = $(this).attr("href"); // We add href value into $hash object $(this).css("color","green"); // Way to mark selected ones $(this).attr("data-pack", "selected"); // Change data-pack property value to selected return false; // We don't want to execute this yet } else if ( $(this).attr("data-pack") == "selected" ) { // In case you change your mind and want to unselect $(this).attr("data-pack", "true"); // Change data-pack property back, thanks to Ambrosia pointing it out in the comment $(this).css("color","red"); // We mark it as unset delete $hash[$(this).attr("id")]; // Remove it from hash return false; } }); $("form").submit( function(){ // After we submit for (var i in $hash) { // Go trough $hash window.open($hash[i]); // And open window for each member } return false; // We don't actually want to submit form, just open new windows :) } ); }); 

HTML看起来像:

     Page Title     data data data data ordinary link 

我相信最简单的方法是使用锚标记的target="_blank"属性。 创建元素的集合并通过它.click

理论上你可以使用window.open但是这个方法会打开一个新窗口而不是你想要的新TAB。