可以将通用选择器*替换为:lang? [英] Can the Universal selector * be replaced by :lang?

查看:115
本文介绍了可以将通用选择器*替换为:lang?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通用选择器星号(*)是唯一的,因为它匹配任何类型的单个元素。

The universal selector asterisk (*) is unique in that it matches a single element of any type.

所以如果我有一个div不同的元素,使用一个选择器选择它们,我可以添加一个类到所有的内部元素(像 .parent .class {} )或者我可以使用通用选择器 .parent * {}

So if I have different elements within a div and I want to select them all with one selector, I can either add a class to all the inner elements (something like .parent .class {}) or I can use the universal selector (.parent * {})

然后我看到用于:lang伪元素(特别是结束)的规范


注意[lang | = xx]和:lang(xx)之间的区别。在这个HTML
示例中,只有BODY匹配[lang | = fr](因为它有一个LANG
属性),但BODY和P匹配:lang
是法语
)。



<body lang=fr>
  <p>Je suis Français.</p>
</body>

这意味着:lang 选择器也是目标。 (哇!)

Which means that all elements within an element targeted by :lang selector are also targeted. (Wow!)

所以让我们想要给一个div中的所有元素添加一个边框,而不是选择器 div * {} ::lang

So let's say I wanted to add a border to all the elements within a div - instead of the selector div * {} I could theoretically use :lang

据我所知,唯一的区别是:lang 选择器选择父级以及所有子级(当然还有技术差异:lang有更大的特异性)....

As far as I can tell, the only difference is that the :lang selector selects the parent as well as all the children (and of course there's the technical difference the :lang has greater specificity)....however

如果:lang选择器以语义方式应用,它包含整个文档 - 在html上使用lang 属性元素 - 我不认为上述差异很重要。

if the :lang selector was applied in a semantic way that it included the whole document - with the lang attribute on the html element - I don't think that the above difference would matter.

所以基本上我的问题是:

So basically my question is:

假设我的html元素有属性 lang =en

Assuming that my html element has the attribute lang="en":

选择器如:

* { box-sizing: border-box; }

:lang(en) {
  box-sizing: border-box;
}

代码似乎工作了( DEMO ),它似乎也是语义的,但我想知道是否有某些原因/缺点上述技术。

The code seems to work (DEMO), and it seems to be semantic as well, but I'm wondering if there are certain reasons/cons to the above technique.

推荐答案


通用选择器*可替换为:lang?

Can the Universal selector * be replaced by :lang?

不,因为你不能使用:lang()写一个选择器,你假设文档中的所有元素总是以相同的语言。 1

No, because you cannot write a selector using :lang() that is guaranteed to match all elements unless you assume all elements in the document will always be in the same language.1

如果你假设所有元素都在相同的语言,那么使用:lang() pseudo是毫无意义的,因为该伪类的整个要点是能够区分文档的不同部分

If you're going to assume that all elements are in the same language, then using the :lang() pseudo is pretty pointless, since the whole point of that pseudo-class is to be able to distinguish parts of the document that differ in their content language.

另请注意,复合选择器:lang(en)简单选择器)相当于 *:lang(en)。它本质上是具有伪类的附加资格的 * 选择器。您不能避免使用 * 替换为:lang()

Also note that the compound selector :lang(en) (consisting of just that one simple selector) is equivalent to *:lang(en). It is essentially the * selector with an additional qualification of a pseudo-class. You are not avoiding the use * by replacing it with :lang().

1 选择器4 允许编写与任何语言中的元素匹配的:lang('*') (如果你不在乎元素是什么语言,这也是无意义的),但这假设文档甚至内置了内容语言语义。不清楚:lang()是否会在缺少这种语义的文档中工作。

1 Selectors 4 allows a selector like :lang('*') to be written that matches elements in any language (which, again, is pointless if you don't care what language an element is in!), but this assumes the document even has content language semantics built into it. It is not clear if :lang() will work at all in a document lacking such semantics.

这篇关于可以将通用选择器*替换为:lang?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆