jQuery选择没有类或ID的元素 [英] jQuery Select Element that has no Class or ID
问题描述
尝试查找选择器以获取既没有类也没有设置id的所有元素。
Trying to find a selector to get all elements that have both no class, and no id set to them.
到目前为止,我有两种不同的输出,具体取决于选择器中是否有空格:
So far I have 2 different outputs depending on if there is a space in the selector:
// outputs
var noID = $('*:not([id])');// 144 - may have a class
var noClass = $('*:not([class])'); // 100 - may have an id
var withSpace = $('*:not([id]) *:not([class])'); // 99 ?
var noSpace= $('*:not([id])*:not([class])'); // 84 ?
哪一个是正确的,我的猜测是 noSpace
- 但我不知道。以前有人试过吗?
Which one is correct, my guess is the noSpace
- but I don't know. Anyone tried this before?
我的猜测是,对于空格,选择器进入没有ID的标签内,并选择没有关联类的子元素与他们一起。
My guess is that with the space, the selector is going inside the tag that has no ID, and selecting the children elements that have no class associated with them.
noSpace
结果是正确的,因为它只选择两个都没有的元素类,没有id。
And the noSpace
result is the correct one, as it selects only the elemets that have both no class, and no id.
有人可以验证吗?谢谢!
Can someone verify? Thanks!
使用此选择器查找元素既没有类,也没有与之关联的id。
Use this selector to find elements that have neither a class, or an id associated with them.
$('*:not([id]):not([class])');
奖金: $('body *:not([id]): not([class])');
- 如果你只想处理实际内容
bonus: $('body *:not([id]):not([class])');
- If you only want to deal with the actual content
推荐答案
withSpace - $('*:not([id])*:not([class])');
将找到所有没有类的元素没有ID的元素。在选择器中添加一个空格就像分别调用find一样。
withSpace - $('*:not([id]) *:not([class])');
will find all elements with no class that are inside an element without an ID. Putting a space in the selector is like calling find seperately.
你可以改变noSpace来改变它,但仍然得到正确的结果:
You could change noSpace to be this instead and still get the right result:
var noSpace= $('*:not([id]):not([class])'); // second * not needed
这篇关于jQuery选择没有类或ID的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!