是否有任何理由使用选择器“*.class”?超过“.class”?
我在代码中找到了它,但不知道使用 *.class
选择器而不是 .class
选择器有什么意义。
I found it in code and don't know what's the point of using *.class
selector over .class
selector.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
无论是在 jQuery (Sizzle) 选择器上下文还是 CSS 选择器上下文中,它都没有意义。
*.class
和.class
在元素匹配和特异性方面是等效的。*
字符仅在单独使用来引用任何元素时才有意义;当您使用任何其他不带类型选择器的简单选择器时,就暗示了这一点。 即使如此,您也很少会发现自己需要将事件挂钩或操作 DOM 上每个元素的属性。如果您打算使用类、伪类、ID 等,那么在那里使用通用选择器是没有意义的,因为所选元素无论如何都会被相应地过滤。
此外,这里还有一个将
*
与其他选择器一起使用的缺点,引用自 jQuery 文档< /a>:这是选择器规范的内容:
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:And here's what the Selectors spec says:
它们大致相同 - 星号仅进一步指定将选择应用到所述类的哪些对象。
They are roughly equivalent - the asterisk only further specifies which objects of said class to apply the selection to.
不,没有理由指定
*.class
而不是.class
*
是通配符,意味着“将其应用于任何内容”,但这就是只是本机选择器行为。然而,它可能是由那些试图在面对以下情况时明确记录的人添加的:
但这仅在您的内部正常代码组织属于以下类型时才成立:
等
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:
But this only holds true if your internal normal code-organization is of the type:
etc
不会获得任何优势 - 我认为如果您将通配符与其他选择器一起使用,则会出现性能问题:
除此之外——没有区别
There would be no advantage gained - I think there is a performance issue if you use wildcard with further selectors:
Other than that - no difference