简体   繁体   English

让<a>标签打开两个网址(在两个不同的页面)</a>

[英]Make the <a> tag open two URLs (in two different pages)

I wanted to make the tag open two URLs at the same time.我想让标签同时打开两个网址。 This is what I tried:这是我试过的:

Only HTML只有 HTML

<a href="URL1" target="_blank" onclick="window.open('URL2');">text</a>

This did work but not the way I wanted it to.这确实有效,但不是我想要的方式。 It would open the URL2 when clicked on it and if opened in a new tab with right click or the mouse wheel it would open URL1.单击它会打开 URL2,如果通过右键单击或鼠标滚轮在新选项卡中打开它会打开 URL1。 I want it to open both pages in new tabs at the same time.我希望它同时在新标签页中打开两个页面。

HTML + JavaScript HTML: HTML + JavaScript HTML:

<a id="myId">text</a>

JS:记者:

myId.onclick = function(){
open('https://www.example1.com');
location.href = ('https://www.example2.com');
}

This didn't work at all.这根本不起作用。

This is Your code:这是你的代码:

     myId.onclick = function(){`enter code here`
        open('https://www.example1.com');
        location.href = ('https://www.example2.com',,'_blank');
        }

Change the code to:将代码更改为:

    myId.onclick = function(){ 
    window.open('https://www.example1.com','_blank');  //just use window.open() for both the cases;
    window.open('https://www.example2.com');
    }

Hope, you have got the solution for your problem.希望,您已经为您的问题找到了解决方案。

Try using an onclick function that uses the window.open method to open the two URLS:尝试使用 onclick function 使用window.open方法打开两个 URL:

    document.querySelector("#foo").onclick = function () {
      window.open('https://www.example.com', "_blank");
      window.open('https://www.example.com', "_blank");
    }


    <a id="foo">bar</a>

As per your requirement, I would suggest following.根据您的要求,我建议您遵循。 Also look at the fiddle HERE也看看这里的小提琴

<a href="#" id="myId">Open Two URLs</a>

var myId = document.getElementById("myId");
myId.onclick=function(){
  window.open("https://www.google.com","_blank");
  window.open("https://www.microsoft.com","_blank");
}

You should be allowing your browser's POPUP BLOCKER to allow opening multiple pages/tabs for this to work.您应该允许您的浏览器的POPUP BLOCKER允许打开多个页面/选项卡以使其工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM