我有两个(或更多)链接,比如:http://google.com和http://yahoo.com。
如何让它们在单击一个链接时同时打开?
例如,一个名为“点击此处”的链接,当单击时将打开两个不同的空白窗口。
我有两个(或更多)链接,比如: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
有时会被弹出窗口拦截器和/或广告过滤器阻止。
以下是来自 Paul 的补充说明:这种方法还需要启用 JavaScript。通常不是个好主意,但有时候是必要的。
yourlink
),然后使用jQuery将它们组合成一个打开所有URL的链接。这样,您可以在HTML中指定URL,而不是在JavaScript中指定。 - Paul D. Waite我以简单的方式完成了它:
<a href="http://virtual-doctor.net" onclick="window.open('http://runningrss.com');
return true;">multiopen</a>
它将在新窗口打开runningrss,并在同一窗口中打开virtual-doctor。
如果您希望即使JavaScript未启用,用户仍然可以打开所有链接,则可能希望重新排列您的HTML。 (我们称之为渐进增强)。 如果是这样,类似以下内容可能效果很好:
<ul class="yourlinks">
<li><a href="http://www.google.com/"></li>
<li><a href="http://www.yahoo.com/"></li>
</ul>
$(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.
});
这段代码假设你只想在每个页面上使用一个类似于“多链接”的东西。(我也没有测试过它,所以它可能充满了错误。)
<a href="http://--"
onclick=" window.open('http://--','','width=700,height=700');
window.open('http://--','','width=700,height=500'); ..// add more"
>Click Here</a>`
<a href="" id="multi-link-opener" onclick="openMultipleLinks(myLinks)">Click To Open Links</a>
JS
var myLinks = [
"https://google.com",
"https://www.w3schools.com/jsref/met_win_open.asp",
"https://developer.mozilla.org/en-US/docs/Web/API/Window/open"
]
function openMultipleLinks(links) {
for (var i = 0; i < links.length; i ++) {
window.open(links[i]);
}
}
我采用了一种介于Paul和Adam方法之间的混合方法:
打开链接数组的链接已经在HTML中。jQuery只是创建链接数组,并在单击“全部打开”按钮时打开每个链接:
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>
$(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...
}
});
});
如果您希望通知访问者将要打开哪些链接,您可以使用一个JS函数从HTML元素中读取链接。您甚至可以让访问者像下面所示一样编写/修改链接:
<script type="text/javascript">
function open_all_links() {
var x = document.getElementById('my_urls').value.split('\n');
for (var i = 0; i < x.length; i++)
if (x[i].indexOf('.') > 0)
if (x[i].indexOf('://') < 0)
window.open('http://' + x[i]);
else
window.open(x[i]);
}
</script>
<form method="post" name="input" action="">
<textarea id="my_urls" rows="4" placeholder="enter links in each row..."></textarea>
<input value="open all now" type="button" onclick="open_all_links();">
</form>