简体   繁体   English

使用 querySelector 获取包含类的所有元素

[英]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.

相关问题 document.querySelector获取包含括号的类名 - document.querySelector get class name containing parentheses 如何获取 HTML 元素但使用 querySelector 排除所有子元素 - How to get the HTML element but exclude all child elements with querySelector AngularJS-如何通过q​​uerySelector获取所有元素并禁用链接 - AngularJS - how to get all elements by querySelector and disable links 具有属性类的所有元素不包含“已禁用” - All elements with attribute class not containing “disabled” 如何在 JavaScript 中使用 querySelector 选择具有特定类的所有子元素,而不考虑它们在 HTML 中出现的顺序? - How can I select all child elements with a certain class using querySelector in JavaScript regardless of the order they appear in HTML? Document.querySelector() 未显示所有元素 - Document.querySelector() is not showing all elements 无法使用 querySelector 获取访问元素? - Can't get access elements with querySelector? 查询选择器获取属性以“xxx”开头的元素 - queryselector get elements where attribute starts with 'xxx' Javascript:querySelector 只选择 1 个 div。 如何将其发送到 select 所有具有相同 class 的 div? - Javascript: querySelector only selects 1 div. How do I get it to select all divs with the same class? 获取具有相同类的所有元素 - Get all elements with the same class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM