Is there any reason to use selector '*.class' over '.class'?
I found it in code and don't know what's the 开发者_如何学Pythonpoint of using *.class
selector over .class
selector.
There's no point to it, whether in the jQuery (Sizzle) selector context or CSS selector context. Both *.class
and .class
are equivalent in terms of elements matched, and specificity.
The *
character is only significant when used by itself to refer to any element; it's implied the moment you use any other simple selector without a type selector. Even then, you will seldom, if ever, find yourself needing to hook events to or manipulate properties of every element on the DOM.
If you are going to use classes, pseudo-classes, IDs and so on, then there's no point having the universal selector there as the selected elements will be filtered accordingly anyway.
Furthermore, here's one shortcoming of using *
with other selectors, quoted from the jQuery documentation:
Caution: The all, or universal, selector is extremely slow, except when used by itself.
And here's what the Selectors spec says:
If a universal selector represented by * (i.e. without a namespace prefix) is not the only component of a sequence of simple selectors selectors or is immediately followed by a pseudo-element, then the * may be omitted and the universal selector's presence implied.
Examples:
*[hreflang|=en]
and[hreflang|=en]
are equivalent,*.warning
and.warning
are equivalent,*#myid
and#myid
are equivalent.
They are roughly equivalent - the asterisk only further specifies which objects of said class to apply the selection to.
.class // Selects all of the specific class
*.class // Selects all of the specific class - where * can be a div, li, etc.
No, there's no reason to specify *.class
over .class
*
is a wildcard character, and means "apply this to anything" but that's just the native selector behavior.
However, it could have been added by someone who was trying to be explicitly documentary in the face of:
Don't select a particular element type, but match all element types
But this only holds true if your internal normal code-organization is of the type:
$('div.class')
$('a.class')
$('a:first')
etc
There would be no advantage gained - I think there is a performance issue if you use wildcard with further selectors:
Caution: The all, or universal, selector is extremely slow, except when used by itself.
Other than that - no difference
精彩评论