[英]Javascript - Create a "a" element and open it in a new tab
I am trying to create a link by means of the tag "a", click this link and open it in a new tab.我正在尝试通过标签“a”创建链接,单击此链接并在新选项卡中打开它。 My script works but does not open the link in a new tab does it on the same page.我的脚本可以工作,但没有在新选项卡中打开链接,但它是在同一页面上执行的。
The script is activated by clicking on the "body" element of the web page.通过单击网页的“正文”元素来激活脚本。 Apparently the target = '_blank' is not working.显然 target = '_blank' 不起作用。
<script>
function openInNewTab(url) {
var a = document.createElement("a");
a.target = '_blank';
a.href = window.location.href;
a.click();
window.location.href = 'https://www.google.com.pe';
}
$("body").click(function () {
openInNewTab();
});
</script>
change your line改变你的线路
a.target = '_blank';
to到
a.setAttribute('target', '_blank');
window.open('https://www.google.com.pe', 'name');
$(document).on('click', 'a', function(e){
e.preventDefault();
var url = $(this).attr('href');
window.open(url, '_blank');
});
Just use window.open(url,'_blank');
只需使用window.open(url,'_blank');
with target blank parameter . 目标空白参数。
function openInNewTab(url) { window.open(url, '_blank'); } $("body").click(function () { url='https://www.google.com.pe/'; openInNewTab(url); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <body> <h1>body</h1> </body>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.