【问题标题】:Opening multiple links in new tabs with one click一键在新标签页中打开多个链接
【发布时间】:2011-06-28 20:58:48
【问题描述】:

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

谢谢

【问题讨论】:

    标签: php javascript jquery foreach


    【解决方案1】:

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

    【讨论】:

    • 它没有。此代码可在 Opera 和 IE8 上一次性使用。我在 FF 和 Chrome 上尝试了很多东西,但没有运气。我对其进行了一些编辑:pastebin.com/1HsBuix9,但它不起作用。我不想让用户问他们是否想要这个链接和那个链接,他们只需按下提交,它就会全部打开。
    • 我的意思是,用不多的话说,这与浏览器制造商和最终用户的理念背道而驰。您如何确保最终用户实际上想要打开多个链接?当您可以回答这个问题时,您就可以回答自己的问题了。
    • 因为是订阅线程的东西,所以可以一键打开所有未读线程...
    【解决方案2】:

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

    为我工作。

    【讨论】:

    • 这在 chrome 中不起作用,如果你指导我解决方案是什么
    猜你喜欢
    • 1970-01-01
    • 2014-08-13
    • 2013-11-30
    • 1970-01-01
    • 1970-01-01
    • 2011-09-29
    • 1970-01-01
    • 1970-01-01
    • 2020-07-27
    相关资源
    最近更新 更多