【问题标题】:Don't know how to use target="_blank" in code不知道如何在代码中使用 target="_blank"
【发布时间】:2021-01-16 13:28:09
【问题描述】:

我目前使用一个下载按钮,其 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 秒计时器开始计时,完成后,链接在同一页面中打开。

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

请帮忙。

【问题讨论】:

  • window.location.replace(a) 更改为 window.open(a)
  • 嘿,罗里·麦克罗桑!太感谢了。 ☺♥ 这对我有用。 ☺♥非常感谢☺♥
  • 很高兴它有帮助。我为你添加了答案

标签: javascript html jquery css json


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    target="_blank" 是对应于“a”元素的属性。如果单击链接元素,它将触发在新选项卡中打开链接。

    【讨论】:

    • 请为 Asker 和他的问题“如何在代码中使用”提供有用的代码,而不是它的用途......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多