简体   繁体   English

如何打开一个新的 html 页面以及一键点击链接

[英]How to open a new html page and also a link at one button click

So, what I want to do is open a URL that I am providing in href and at the same time open a new html page.所以,我想做的是打开我在href中提供的 URL,同时打开一个新的 html 页面。 On button click it leads to the URL in a new window with target="_blank" .单击按钮时,它会导致新的 window 中的 URL 和target="_blank" But what I want to do is open the link in the new window and also open a new HTML page in the previous window.但是我想做的是在新的window中打开链接,同时在之前的window中打开一个新的HTML页面。

Below is my code,下面是我的代码,

<li class="search-li">
    <div class="collapsible-header collapsible-noborder sakura-lighter-bg">
        <i class="fa fa-mobile-alt" aria-hidden="true"></i>TEST LIST
    </div>
    <div class="collapsible-body collapsible-noborder sakura-midlight-bg">
        <button class="primary download">
            <a id="buttton1" class="buttton7" target="_blank" href="example.com">Download</a>
        </button>
    </div>
</li>

Something like this像这样的东西

Test page测试页

The click will open a new window with the data-href and change existing window with href单击将使用 data-href 打开一个新的 window 并使用 href 更改现有的 window

 window.addEventListener("load", function() { document.getElementById("button1").addEventListener("click", function() { window.open(this.getAttribute("data-href"), "_blank") }) })
 <li class="search-li"> <div class="collapsible-header collapsible-noborder sakura-lighter-bg"> <i class="fa fa-mobile-alt" aria-hidden="true"></i>TEST LIST </div> <div class="collapsible-body collapsible-noborder sakura-midlight-bg"> <a id="button1" class="primary download button7" data-href="https://example1.com" href="https://example2.com">Download</a> </div> </li>

You need to use window.open() method.您需要使用 window.open() 方法。

 <li class="search-li"> <div class="collapsible-header collapsible-noborder sakura-lighter-bg"> <i class="fa fa-mobile-alt" aria-hidden="true"></i>TEST LIST </div> <div class="collapsible-body collapsible-noborder sakura-midlight-bg"> <button class="primary download"> <a onclick="window.open('http://google.com');" id="buttton1" class="buttton7" href="https://www.w3schools.com" >Download</a> </button> </div> </li>

Below should do the job:下面应该做的工作:

<a onclick="window.open('http://google.com');" id="buttton1" 
     class="buttton7" href="http://yahoo.com">Download</a>

No need of _blank since window.open() does that for us, while the current window updates its location from href attribute不需要_blank ,因为window.open()为我们做了这个,而当前的 window 从href属性更新了它的位置

You can use This你可以使用这个

<button onclick="window.location.href='page2.html'">Go to another page in your folder</button>

or you can use a link like that或者你可以使用这样的链接

<button onclick="window.location.href='https://example.com/'">Go to another page in an URL</button>

 <.DOCTYPE html> <html> <body> <li class="search-li"> <div class="collapsible-header collapsible-noborder sakura-lighter-bg"> <i class="fa fa-mobile-alt" aria-hidden="true"></i>TEST LIST </div> <div class="collapsible-body collapsible-noborder sakura-midlight-bg"> <button class="primary download"> <a id="buttton1" onmouseup="window.location.replace('/blank;html')." class="buttton7" target="_blank" href="example.com">Download</a> </button> </div> </li> </body> </html>

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

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