打开多个链接与点击
问题描述:
我有一些PHP代码:打开多个链接与点击
foreach($moduid as $k=>$mod) {
$random = $k+1;
echo '<a href="http://mysite.com?y='.$cid.'&t='.$mod.'&s=-2" data-pack="true" id="link'.$random.'">data</a>';
}
和JS代码:
$(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 :)
});
});
我已经使用了一些这样的:Open Links in Multiple Browser Windows/Tabs
然而,似乎没有工作,当我点击提交。我不太了解JS,并希望有人知道为什么按提交不会在新标签中打开所有这些链接。我正在使用这个jQuery - http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js
它在IE8中工作,但不是Firefox和Chrome,我希望它打开所有链接,不仅仅是我选择的链接。所以也许这不是正确的工作JS?
在Firefox中,它只是在链接之后。
谢谢
答
去取消阻止弹出窗口,你的代码应该工作。
答
HTML:
<body>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js" type="text/javascript" charset="utf-8"></script>
</head>
<?php
foreach($moduid as $k=>$mod) {
$random = $k+1;
echo '<a href="http://mysite.com?y='.$cid.'&t='.$mod.'&s=-2" data-pack="true" id="link'.$random.'">data</a>';
}
?>
</body>
JS:
$("form").submit(function(){
alert('asdf');
$('a').each(function(){
$(this).attr('target','_blank');
window.open($(this).attr('href'));
})
return false;
});
为我工作。
它不。此代码适用于Opera和IE8一次性使用。我在FF和Chrome上尝试了很多东西,但没有运气。我编辑了一下:http://pastebin.com/1HsBuix9,它不工作。我不希望用户询问他们是否想要这个和那个链接,他们只需按下提交并打开所有。 – Dean 2011-02-17 06:16:37
我说的并非如此之多,而是与浏览器制造商和最终用户的哲学背道而驰。您如何确保最终用户实际上想要打开多个链接?当你可以回答这个问题时,你回答自己的问题。 – Lance 2011-02-17 06:28:49