javascript - 不知道如何在代码中使用 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 秒计时器,完成后,在同一页面中打开链接。
我希望该链接在新选项卡中打开,而不是在同一页面中打开,我不知道该怎么做。
请帮忙。
解决方案
要将行为从重定向页面更改为打开新选项卡,window.location.replace(a)
请将代码更改为window.open(a)
.
推荐阅读
- java - Spring boot kafka hello world 应用程序无法以“无法将属性绑定在...下...”
- sql-server - 从 Oracle 端的 MSSQL 接收 @@IDENTITY
- javascript - 如何在单列材料表中编辑多个字段
- javascript - 如何从类实例中获取 JavaScript 类构造函数参数
- node.js - UnhandledPromiseRejectionWarning:TypeError 无法读取未定义的属性“名称”
- vesta - Cannot setup VestaCP in fresh CentOS8. Failed to synchronize cache for repo 'vesta'
- google-apps-script - 如何在谷歌工作表中创建函数睡眠(毫秒)以延迟或暂停某个范围内的公式
- django - Django unknown column error when trying to get a model objects
- java - 如果成员变量关闭,如何在不将类型从 void 更改为 boolean 的情况下显示 true?
- java - 与他人共享图像和文本应用程序