点击链接时如何打开多个页面

我有两个(或更多)的链接。 例如: http : //google.com和http://yahoo.com 。

我怎样才能让他们打开时,我点击一个单一的链接?

例如,一个名为“点击这里”的链接,点击后,将打开两个不同的空白窗口。

HTML:

<a href="#" class="yourlink">Click Here</a> 

JS:

 $('a.yourlink').click(function(e) { e.preventDefault(); window.open('http://yoururl1.com'); window.open('http://yoururl2.com'); }); 

window.open也可以采取额外的参数。 看到他们在这里: http : //www.javascript-coder.com/window-popup/javascript-window-open.phtml

你也应该知道window.open有时被popup窗口阻止程序和/或广告filter阻止。

来自Paul的补充说明:这种方法也对JavaScript的启用依赖。 通常不是一个好主意,但有时是必要的。

我以一种简单的方式做到了这一点:

  <a href="http://virtual-doctor.net" onclick="window.open('http://runningrss.com'); return true;">multiopen</a> 

它会在一个新的窗口和虚拟医生在同一个窗口中打开跑步者 。

您可能想安排您的HTML,以便用户仍然可以打开所有的链接,即使JavaScript没有启用。 (我们称之为渐进式增强。)如果是这样,这样的事情可能会运作良好:

HTML

 <ul class="yourlinks"> <li><a href="http://www.google.com/"></li> <li><a href="http://www.yahoo.com/"></li> </ul> 

jQuery的

 $(function() { // On DOM content ready... var urls = []; $('.yourlinks a').each(function() { urls.push(this.href); // Store the URLs from the links... }); var multilink = $('<a href="#">Click here</a>'); // Create a new link... multilink.click(function() { for (var i in urls) { window.open(urls[i]); // ...that opens each stored link in its own window when clicked... } }); $('.yourlinks').replaceWith(multilink); // ...and replace the original HTML links with the new link. }); 

这段代码假设你只想每页使用一个这样的“多重链接”。 (我也没有testing过,所以可能是错误的。)

你可以单击打开多个窗口…试试这个..

 <a href="http://--" onclick=" window.open('http://--','','width=700,height=700'); window.open('http://--','','width=700,height=500'); ...// add more">Click Here</a>` 

我在Paul和Adam的方法之间创造了一个混合的方法:

打开链接数组的链接已经在html中。 jquery只是创build链接数组,并打开每个“打开所有”button时单击:

HTML:

 <ul class="links"> <li><a href="http://www.google.com/"></a></li> <li><a href="http://www.yahoo.com/"></a></li> </ul> <a id="open-all" href="#">OPEN ALL</a> 

JQUERY:

 $(function() { // On DOM content ready... var hrefs = []; $('.links a').each(function() { hrefs.push(this.href); // Store the URLs from the links... }); $('#open-all').click(function() { for (var i in hrefs) { window.open(hrefs[i]); // ...that opens each stored link in its own window when clicked... } }); }); 

你可以在这里查看: https : //jsfiddle.net/daveaseeman/vonob51n/1/

您需要解除阻止浏览器popup窗口,代码可以工作。

铬://设置/ contentExceptions#popup窗口

Chrome浏览器设置