简体   繁体   English

从某个类中获取除元素以外的所有元素?

[英]Get all elements except ones from a certain class?

I currently have a JavaScript function that uses getElementsByClassName("apples") to find everything in the class apples . 我目前有一个JavaScript函数,该函数使用getElementsByClassName("apples")查找类apples所有内容。 My problem is that I can't seem to find a way to fetch every element not in the class apples . 我的问题是,我似乎无法找到一种方法,每一个元素的类不能获取apples Is there any helper functions in JavaScript that might allow me to do this? JavaScript中是否有任何辅助函数可以允许我执行此操作? Thanks for the help! 谢谢您的帮助!

You can't do it with getElementsByClassName . 您不能使用getElementsByClassName做到这一点。

The easiest way on modern browsers would be to use: 现代浏览器上最简单的方法是使用:

document.querySelectorAll(':not(.apples)')

On older browsers you'd probably have to use document.getElementsByTagName('*') followed by a filtering operation to remove the unwanted elements. 在较旧的浏览器上,您可能必须使用document.getElementsByTagName('*')然后进行过滤操作以删除不需要的元素。 This would probably be very slow. 这可能会很慢。

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

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