[英]HTML Add attribute on an element
當我寫(很多) <a>
標簽時,我沒有寫'target="_blank"'
,所以沒有一個鏈接指向另一個窗口或標簽。
有沒有一種方法可以將"target='_blank'"
到所有使用JavaScript的鏈接中?
您根本不需要JavaScript。 您可以使用head
的base
元素指定錨的基本URL或目標。
<base target="_blank">
將使頁面上的所有鏈接在新窗口和/或選項卡中打開。
有關基本元素的更多信息,請參見MDN 。
先前在這里回答: 如何將target =“ _ blank”添加到指定div中的鏈接?
碼:
/* here are two different ways to do this */
//using jquery:
$(document).ready(function(){
$('#link_other a').attr('target', '_blank');
});
// not using jquery
window.onload = function(){
var anchors = document.getElementById('link_other').getElementsByTagName('a');
for (var i=0; i<anchors.length; i++){
anchors[i].setAttribute('target', '_blank');
}
}
// jquery is prettier. :-)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.