这个jQuery代码的Javascript等价物是什么

这个jQuery代码的Javascript等价物是什么,javascript,jquery,Javascript,Jquery,我正在处理一个项目,不允许使用jQuery。我正在为以下jquery寻找等效的普通Javascript代码: $('img[alt="Apple Store Logo"]').parent().removeAttr("target"); 我发现了以下信息: jQuery Javascript parent() : .parentElement removeAttr() : removeAttribute() 找不到如何构造上述选择器。您可以使用来获取与指定选择器匹配的

我正在处理一个项目,不允许使用jQuery。我正在为以下jquery寻找等效的普通Javascript代码:

$('img[alt="Apple Store Logo"]').parent().removeAttr("target");
我发现了以下信息:

jQuery         Javascript
parent() :     .parentElement
removeAttr() : removeAttribute()
找不到如何构造上述选择器。

您可以使用来获取与指定选择器匹配的值。然后实现对所有元素进行迭代,以使用 和

请尝试以下操作:

document.querySelectorAll('img[alt="Apple Store Logo"]').forEach(function(el){
  el.parentNode.removeAttribute("target");
});
某些较旧的浏览器不支持在浏览器上运行。在这种情况下使用,以实现:


调用的第一部分是一个简单的DOM查询,它检索所有具有与“Apple Store徽标”匹配的alt属性的
元素

这可以很容易地在vanilla JS中完成:

document.querySelectorAll('img[alt="Apple Store Logo"]')
但是,
document.querySelectorAll
返回节点列表,而不是数组。我们可以通过在节点列表上调用
Array.prototype.slice
将其转换为数组:

Array.prototype.slice.call(document.querySelectorAll('img[alt="Apple Store Logo"]'))
我对jQuery的了解很差,但我相信剩余的方法是针对查询结果中返回的元素进行迭代调用的。因此,假设目的是获取对
img
元素的父元素的引用,并删除其
target
属性,则整个代码块可以转换为:

Array.prototype.slice.call(document.querySelectorAll('img[alt="Apple Store Logo"]')).forEach(function (img) {
    img.parentNode.removeAttribute('target');
});

请格式化您的代码-它使阅读您的问题更容易。此外,如果你表现出努力,你可能会得到更多的帮助javascript@Utkanos:已编辑。很抱歉,您可以使用
getElementsByTagName
并在所有标记中循环查找具有匹配alt文本的标记,或者使用
querySelector
$(选择器)===document.querySelectorAll(选择器)
jQuery选择器能够进行迭代逻辑,但是
querySelector
只选择与选择器匹配的第一个元素
querySelectorAll
然后遍历返回的节点列表可能是更安全的方法。并非所有客户端都支持对节点列表的
Array.prototype.forEach()方法调用。为了确保可以使用它,应该将节点列表转换为数组:
array.prototype.slice.call(document.querySelectorAll(selector)).forEach()@Mamun:成功了!谢谢你,先生@泰瑞:谢谢!成功了@斯科特马库斯,从外表上看,就是泰尔。我也为他工作过。非常感谢你@斯科特·马库斯谢谢你!
Array.prototype.slice.call(document.querySelectorAll('img[alt="Apple Store Logo"]'))
Array.prototype.slice.call(document.querySelectorAll('img[alt="Apple Store Logo"]')).forEach(function (img) {
    img.parentNode.removeAttribute('target');
});