开发者

Opening multiple links in new tabs with one click

I have some PHP code:

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

And JS code:

$(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]); // A开发者_运维百科nd open window for each member
    }
    return false; // We don't actually want to submit form, just open new windows :)
} );        
});

I have used some of this: Open Links in Multiple Browser Windows / Tabs

However it doesn't appear to work, when I click submit. I don't really understand JS and was hoping someone would know why pressing submit doesnt open all these links in new tabs. I'm using this jQuery - http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js

It's working in IE8 but not Firefox nor Chrome, I want it to open all links not just ones I've selected. So perhaps this isnt the right JS for the job?

In Firefox it just follows the link.

Thanks


Go unblock popups and your code should work.


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

Worked for me.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜