简体   繁体   English

如何将target =“ _ blank”添加到具有特定类名的div中的链接?

[英]How do I add target=“_blank” to a link within a div with an specific class name?

I´m trying to add target=“_blank” to all the links within the divs with an specific class name. 我正在尝试使用特定的类名将target =“ _ blank”添加到div中的所有链接。 I know how to do it with an ID: 我知道如何使用ID:

window.onload = function(){
  var anchors = document.getElementById('link_other').getElementsByTagName('a');
  for (var i=0; i<anchors.length; i++){
    anchors[i].setAttribute('target', '_blank');
  }
}

But i´m trying to replicate the same, using classes instead of IDS. 但是我试图使用类而不是IDS复制相同的内容。 Any ideas of a how to do this without jquery?. 有没有jquery如何做到这一点的任何想法?

Thanks in davanced! 谢谢!

You can use querySelectorAll() and include a CSS selector. 您可以使用querySelectorAll()并包含CSS选择器。 So if your class name is link-other: 因此,如果您的班级名称是link-other:

 document.querySelectorAll('.link-other a') .forEach(function(elem) { elem.setAttribute('target', '_blank'); }) 
 <div class="link-other"> <a href="http://wikipedia.com">Wikipedia</a> <a href="http://google.com">Google</a> </div> 

You can use document.querySelectorAll() which takes a css expression: 您可以使用带有CSS表达式的document.querySelectorAll()

var anchors = document.querySelectorAll('.my_class a');
for (var i=0; i<anchors.length; i++){
  anchors[i].setAttribute('target', '_blank');
}

Or (ab)using the array prototype: 或者(ab)使用数组原型:

[].forEach.call(document.querySelectorAll('.my_class a'), function(el) {
  el.setAttribute('target', '_blank');
});

You should also consider using addEventListener instead of window.onload : 您还应该考虑使用addEventListener而不是window.onload

window.addEventListener('load', function() {
  // ...
});

Or the more appropriate: 或更合适的是:

document.addEventListener('DOMContentLoaded', function() {
  // ...
}); 

You can also use the old school <base> element which will can set defaults for all a tags: 您还可以使用旧式的<base>元素,该元素可以为所有标签设置默认值:

var base_el = document.createElement('base');
base_el.setAttribute('target', '_blank');
document.head.appendChild(base_el);

Use querySelectorAll and loop just like you did. 使用querySelectorAll并像您一样循环。

var anchors = document.querySelectorAll(".link_other a");

Or you can use getElementsByClassName and nested loops. 或者,您可以使用getElementsByClassName和嵌套循环。

var parents = document.getElementsByClassName(".link_other");
for (var i = 0; i < parents.length; i++) {
    var anchors = parents[i].getElementsByTagName("a");
    for (var j = 0; j < anchors.length; j++) {
        anchors[j].setAttribute('target', '_blank');
    }
}

To achieve your expected result use below option 为了达到预期的效果,请使用以下选项

        var addList = document.querySelectorAll('.link_other a');

    for(var i in addList){
     addList[i].setAttribute('target', '_blank');
    }

Codepen - http://codepen.io/nagasai/pen/QEEvPR Codepen- http: //codepen.io/nagasai/pen/QEEvPR

Hope it works 希望能奏效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM