[英]Get all elements containing a class with querySelector
For changing some styles in a class I'm using querySelector()
:为了更改类中的某些样式,我正在使用
querySelector()
:
el.querySelector('.fa fa-car').style.display = "none";
This works fine for one element but if there are more elements containing this class and I want to change the display to none for all of them, this command only deletes the first occurrence of it leaving the next ones untouched.这适用于一个元素,但如果有更多元素包含此类,并且我想将所有元素的显示更改为无,此命令只会删除它的第一次出现,而下一个则保持不变。
I tried to do it with querySelectorAll()
:我试图用
querySelectorAll()
做到这一点:
el.querySelectorAll('.fa fa-car').style.display = "none";
But this one returns an error message:但这会返回一条错误消息:
html2canvas: TypeError: Cannot set property 'display' of undefined
html2canvas: TypeError: 无法设置未定义的属性“显示”
any ideas about how to get all the elements containing a specific class and perform an operation on them?关于如何获取包含特定类的所有元素并对它们执行操作的任何想法?
The Element method
querySelectorAll()
returns a static (not live) NodeList representing a list of the document's elements that match the specified group of selectors.Element 方法
querySelectorAll()
返回一个静态(非活动)NodeList,表示与指定选择器组匹配的文档元素列表。
Use Document.querySelectorAll()
to get all the elements.使用
Document.querySelectorAll()
获取所有元素。 Then use forEach()
to set the style in each element:然后使用
forEach()
设置每个元素的样式:
var elList = document.querySelectorAll(.fa.fa-car);
elList.forEach(el => el.style.display = "none");
Please Note: Some older version of IE (<8) will not support querySelectorAll()
.请注意:一些旧版本的 IE (<8) 将不支持
querySelectorAll()
。 In that case use在这种情况下使用
Array.from(document.querySelectorAll(.fa.fa-car))
querySelectorAll()
returns a collection of elements. querySelectorAll()
返回元素的集合。 To change their styling you need to loop through them.要更改它们的样式,您需要遍历它们。
Also note that your selector appears to be invalid.另请注意,您的选择器似乎无效。 Given the FontAwesome class rules I presume you need to select by both classes.
鉴于 FontAwesome 类规则,我认为您需要通过两个类进行选择。 Try this:
尝试这个:
Array.from(el.querySelectorAll('.fa.fa-car')).forEach(function() {
this.style.display = "none";
});
Alternatively, as you've tagged the question with jQuery, you could just simplify all that to just:或者,当您使用 jQuery 标记问题时,您可以将所有这些简化为:
$('.fa.fa-car').hide();
querySelector 只选择一个元素,要选择所有元素,您可以使用 querySelectorAll
[].map.call(el.querySelectorAll('.fa fa-car'), n => { n.style.display = 'none'; })
Use method querySelectorAll() See https://www.w3schools.com/jsref/met_document_queryselectorall.asp使用方法 querySelectorAll() 见https://www.w3schools.com/jsref/met_document_queryselectorall.asp
You are getting error because querySelectorAll returns an array.Iterate over it and then use style.display您收到错误,因为 querySelectorAll 返回一个数组。迭代它然后使用 style.display
You could do all your operation by iterating the occurence of this class and of course by help of some if clauses.您可以通过迭代此类的出现来完成所有操作,当然还可以借助一些 if 子句。
$('.fa.fa-car').each(function(){
$(this).css('color','white');
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.