是否有任何理由使用选择器“*.class”?超过“.class”?

发布于 2024-10-25 04:36:24 字数 81 浏览 0 评论 0原文

我在代码中找到了它,但不知道使用 *.class 选择器而不是 .class 选择器有什么意义。

I found it in code and don't know what's the point of using *.class selector over .class selector.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

弄潮 2024-11-01 04:36:24

无论是在 jQuery (Sizzle) 选择器上下文还是 CSS 选择器上下文中,它都没有意义。 *.class.class 在元素匹配和特异性方面是等效的。

* 字符仅在单独使用来引用任何元素时才有意义;当您使用任何其他不带类型选择器的简单选择器时,就暗示了这一点。 即使如此,您也很少会发现自己需要将事件挂钩或操作 DOM 上每个元素的属性。

如果您打算使用类、伪类、ID 等,那么在那里使用通用选择器是没有意义的,因为所选元素无论如何都会被相应地过滤。

此外,这里还有一个将 * 与其他选择器一起使用的缺点,引用自 jQuery 文档< /a>:

警告:全部或通用选择器非常慢,除非单独使用。

这是选择器规范的内容:

如果 * 表示的通用选择器(即没有命名空间前缀)不是 简单选择器序列 选择器或紧随其后的是 伪元素,那么可以省略 * 并暗示通用选择器的存在。

<块引用>

示例:

  • *[hreflang|=en][hreflang|=en] 等效,
  • *.warning.warning 是等效的,
  • *#myid#myid 是等效的。

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.
心凉 2024-11-01 04:36:24

它们大致相同 - 星号仅进一步指定将选择应用到所述类的哪些对象。

 .class // Selects all of the specific class
*.class // Selects all of the specific class - where * can be a div, li, etc.

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.
初相遇 2024-11-01 04:36:24

不,没有理由指定 *.class 而不是 .class

* 是通配符,意味着“将其应用于任何内容”,但这就是只是本机选择器行为。

然而,它可能是由那些试图在面对以下情况时明确记录的人添加的:

不要选择特定元素类型,而是匹配所有元素类型

但这仅在您的内部正常代码组织属于以下类型时才成立:

$('div.class')
$('a.class')
$('a:first')

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

聽兲甴掵 2024-11-01 04:36:24

不会获得任何优势 - 我认为如果您将通配符与其他选择器一起使用,则会出现性能问题:

警告:全部或通用,
选择器非常慢,除了
单独使用时。

除此之外——没有区别

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文