首页 > 解决方案 > 不知道如何在代码中使用 target="_blank"

问题描述

我目前使用一个下载按钮,其 HTML 和 Script 是这样的

<button id="downloadbuttonx" onclick="generate()"><i aria-hidden="true" class="fa fa-cloud-download"></i> Download</button>
<a href="https://www.google.com" id="downloadingx" style="display: none;"><i aria-hidden="true" class="fa fa-cloud-download"></i> Redirecting...</a> Script

<script type='text/javascript'>
  //<![CDATA[
  function generate() {
    var e, n = document.getElementById("downloadingx"),
      t = document.getElementById("downloadbuttonx"),
      a = document.getElementById("downloadingx").href,
      l = 4,
      d = document.createElement("span");
    n.parentNode.replaceChild(d, n), e = setInterval(function() {
      --l < 0 ? (d.parentNode.replaceChild(n, d), clearInterval(e), window.location.replace(a), n.style.display = "inline") : (d.innerHTML = "<i class='fa fa-clock-o' aria-hidden='true'/> Redirecting to download page in " + l.toString() + " seconds.", t.style.display = "none")
    }, 1e3)
  }
  //]]>
</script>

它的工作原理是,当单击下载按钮时,开始 4 秒计时器,完成后,在同一页面中打开链接。

我希望该链接在新选项卡中打开,而不是在同一页面中打开,我不知道该怎么做。

请帮忙。

标签: javascripthtmljquerycssjson

解决方案


要将行为从重定向页面更改为打开新选项卡,window.location.replace(a)请将代码更改为window.open(a).


推荐阅读