Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/470.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何添加target=&x201C_空白”;指向具有特定类名的div中的链接?_Javascript_Class_Hyperlink_Target - Fatal编程技术网

Javascript 如何添加target=&x201C_空白”;指向具有特定类名的div中的链接?

Javascript 如何添加target=&x201C_空白”;指向具有特定类名的div中的链接?,javascript,class,hyperlink,target,Javascript,Class,Hyperlink,Target,我正在尝试将target=“\u blank”添加到具有特定类名的div中的所有链接。我知道如何使用ID进行操作: window.onload = function(){ var anchors = document.getElementById('link_other').getElementsByTagName('a'); for (var i=0; i<anchors.length; i++){ anchors[i].setAttribute('target', '_

我正在尝试将target=“\u blank”添加到具有特定类名的div中的所有链接。我知道如何使用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');
  }
}
window.onload=function(){
var anchors=document.getElementById('link_other').getElementsByTagName('a');
对于(var i=0;i,您可以使用接受css表达式的:

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

您也应该考虑使用“<代码>窗口”。

window.addEventListener('load', function() {
  // ...
});
或更合适的:

document.addEventListener('DOMContentLoaded', function() {
  // ...
}); 
您还可以使用老式的
元素,该元素可以为所有a标记设置默认值:

var base_el = document.createElement('base');
base_el.setAttribute('target', '_blank');
document.head.appendChild(base_el);
您可以使用接受css表达式的:

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

您也应该考虑使用“<代码>窗口”。

window.addEventListener('load', function() {
  // ...
});
或更合适的:

document.addEventListener('DOMContentLoaded', function() {
  // ...
}); 
您还可以使用老式的
元素,该元素可以为所有a标记设置默认值:

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

使用
querySelectorAll
并像以前一样循环

var anchors = document.querySelectorAll(".link_other a");
或者可以使用
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');
    }
}
var parents=document.getElementsByClassName(“.link_other”);
对于(var i=0;i
使用
queryselectoral
并像以前一样循环

var anchors = document.querySelectorAll(".link_other a");
或者可以使用
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');
    }
}
var parents=document.getElementsByClassName(“.link_other”);
对于(var i=0;i
您可以使用querySelectorAll()并包含CSS选择器。因此,如果您的类名为link other:

document.querySelectorAll('.link-other-a')
.forEach(功能(元素){
元素setAttribute('target','u blank');
})

您可以使用querySelectorAll()并包含CSS选择器。因此,如果您的类名为link other:

document.querySelectorAll('.link-other-a')
.forEach(功能(元素){
元素setAttribute('target','u blank');
})

要达到预期效果,请使用以下选项

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

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


希望它能工作

以达到您的预期效果,请使用下面的选项

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

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


希望它能正常工作

更新了代码笔,增加了带有class link\u other的URL,并在没有link\u other class的情况下增加了一些链接。将class更新为“link\u other”以匹配您的代码。请在下面找到更新的代码笔URL,希望这对您有用:)酷…你能把这个问题标记为已回答或投票吗?如果可能的话:)用带有类链接的附加URL更新了代码笔,而没有链接的链接很少。并将类更新为“link\u other”为了与您的代码相匹配。请在下面找到更新的代码笔URL希望这对您有用:)酷…您可以将此问题标记为已回答或投票(如果可能):@user2965583您应该接受最有用的答案。这将不起作用。
。forEach
不是节点列表上的方法。@andlrc您错了。forEach()是数组的一个原型方法。querySelectAll()返回数组的一个实例。单击
运行代码片段
,查看此方法是否确实有效。有关更多信息,请参阅@Martin:但与您相反。
文档。querySelectorAll()
根据DOM规范返回一个
NodeList
。@Martin:也许你有?@user2965583你应该接受最有用的答案。这不会像
那样起作用。forEach
不是NodeList上的方法。@lrc你错了。forEach()是数组的原型方法。querySelectAll()返回数组的一个实例。单击
运行代码片段
以查看此操作是否确实有效。有关更多信息,请参阅@Martin:但与您相矛盾。
document.queryselectoral()
根据DOM规范返回一个
节点列表。@Martin:可能有?