Javascript 在多个浏览器窗口/选项卡中打开链接
我需要单击多个链接(选择它们),然后单击一个按钮,该按钮将在新窗口或选项卡中打开所有选定的链接。这当然取决于浏览器的行为 我的计划是使用Javascript将所选链接添加到数组中,然后单击submit按钮,Javascript将在数组中运行,并为每个项目打开一个新窗口。我可能会在jQuery中这样做Javascript 在多个浏览器窗口/选项卡中打开链接,javascript,jquery,tabs,window,Javascript,Jquery,Tabs,Window,我需要单击多个链接(选择它们),然后单击一个按钮,该按钮将在新窗口或选项卡中打开所有选定的链接。这当然取决于浏览器的行为 我的计划是使用Javascript将所选链接添加到数组中,然后单击submit按钮,Javascript将在数组中运行,并为每个项目打开一个新窗口。我可能会在jQuery中这样做 有人做过类似的事情吗?有其他选择吗?我认为最简单的方法是使用锚定标记的target=“\u blank”属性。创建元素和的集合。单击其中的 理论上你可以使用窗口。open但是这个方法会打开一个新窗口
有人做过类似的事情吗?有其他选择吗?我认为最简单的方法是使用锚定标记的
target=“\u blank”
属性。创建
元素和的集合。单击其中的
理论上你可以使用窗口。open
但是这个方法会打开一个新窗口,而不是你想要的新标签。我认为你是对的
实现您所描述的IMHO的最佳方法是使用return false将您要在新窗口中打开的链接的URL放入一个数组中代码>以防止实际打开链接,然后使用某种循环来打开所有链接
我自由地整理了几行jQuery代码,这些代码将实现您所描述的功能:
$(document).ready(function() {
var $hash = new Array(); // We create new Array
$('a').click( function(){ // On each click to <a> 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 :)
} );
});
$(文档).ready(函数(){
var$hash=new Array();//我们创建一个新数组
$('a')。在每次单击时单击(函数(){//
在单击函数的“else if”部分,您没有将数据包值设置回“true”。++Nice,我正要用HTML代码回答,但您抢先回答了。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Page Title</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js" type="text/javascript" charset="utf-8" async defer></script>
<script src="application.js" type="text/javascript" charset="utf-8" async defer></script>
</head>
<body>
<a href="#link1" data-pack="true" id="link1">data</a>
<a href="#link2" data-pack="true" id="link2">data</a>
<a href="#link3" data-pack="true" id="link3">data</a>
<a href="#link4" data-pack="true" id="link4">data</a>
<a href="#">ordinary link</a>
<form>
<input type="submit" value="submit">
</form>
</body>
</html>