[英]Using target_blank to open a new tab on click - can I load a new page in the original tab automatically?
[英]How can I open a link in a new tab if it has target=_blank?
僅當該鏈接具有像這樣的target=_blank
時,我才嘗試在新選項卡中打開鏈接。
<a href="https://google.com" target="_blank">Google</a>
由於某種原因, 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');
});
這是我使用的腳本,但它沒有任何效果。
將代碼更改為
<a href="https://google.com" class="navigation-title" target="_blank">Google</a>
我在“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.