只需单击一下即可在新选项卡中打开多个链接

时间:2011-02-17 05:56:13

标签: php javascript jquery foreach

我有一些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中,它只是跟随链接。

由于

2 个答案:

答案 0 :(得分:1)

要取消阻止弹出窗口,您的代码应该可以正常工作。

答案 1 :(得分:0)

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;
} );  

为我工作。