[英]How can I open a link in a new tab if it has target=_blank?
I am trying to open a link in a new tab only if that link has a target=_blank
like so.仅当该链接具有像这样的
target=_blank
时,我才尝试在新选项卡中打开链接。
<a href="https://google.com" target="_blank">Google</a>
For some reason the target=_blank
is not working in the a Bootstrap 4.5 navbar Therefore, I am trying to use jQuery to get it to work as some links have the target=_blank
attribute:由于某种原因,
target=_blank
在 Bootstrap 4.5 导航栏中不起作用因此,我尝试使用 jQuery 使其工作,因为某些链接具有target=_blank
属性:
$('a.navigation-title').click(function () {
location.href = this.href;
$(this).find('a').attr('target','_blank');
window.open(this.href, '_blank');
});
That's the script I've used, but its not having any effect.这是我使用的脚本,但它没有任何效果。
Change code to将代码更改为
<a href="https://google.com" class="navigation-title" target="_blank">Google</a>
I have added a new class to the 'a' tag for triggering the click event for jquery我在“a”标签中添加了一个新的 class,用于触发 jquery 的点击事件
$('.navigation-title').click(function () {
var url = $(this).attr('href');
window.open(url , '_blank');
});
var elements = document.querySelectorAll('a[target=_blank]'); for (var index = 0; index < elements.length; index++) { const element = elements[index]; element.addEventListener('click', function (e) { e.preventDefault(); var href = element.getAttribute('href'); if (href) { window.open(href, '_blank'); } return false; }); }
<a href="https://google.com" target="_blank">Google</a>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.