>之间有功能上的区别吗? *:第一个孩子和> :第一个孩子? [英] Is there a functional difference between > *:first-child and > :first-child?
问题描述
在编写代码块时,我注意到在一个地方我写了> :first-child
,后来又写了> *:first-child
.这两个功能块都可以正常工作,但是两者之间有区别吗?
Writing a block of code, I noticed that in one place I wrote > :first-child
and later on > *:first-child
. Both blocks appear to be functional, but is there a difference between the two?
推荐答案
即使我们考虑性能,它们也是相同的.从规范中我们可以阅读
They are identical even if we consider performance. From the specification we can read
如果用*表示的通用选择器(即没有名称空间前缀)不是简单选择器选择器序列的唯一组成部分,或者不是紧随其后的是伪元素,那么* 可以省略并暗示通用选择器的存在.
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.
因此,为浏览器编写> :first-child
的含义应与> *:first-child
相同.
So writing > :first-child
should mean the same as > *:first-child
for the browser.
您还可以阅读
注意:建议不要省略* ,因为这样可以减少例如
div :first-child
和div:first-child
之间的潜在混淆.在这里,div *:first-child
更具可读性.
Note: it is recommended that the * not be omitted, because it decreases the potential confusion between, for example,
div :first-child
anddiv:first-child
. Here,div *:first-child
is more readable.
因此,这不仅是优先事项,而且还有助于避免混乱并提高代码的可读性.
So it's not only a matter of preference but it helps avoid confusion and make the code more readable.
在新的分隔符中,我们还可以阅读:
In the new sepcification we can also read:
除非元素没有特征,否则通用选择器的存在对元素是否与选择器匹配没有影响.
和
注意:在某些情况下,添加通用选择器可以使选择器更易于阅读,即使它对匹配行为没有影响.例如,
div :first-child
和div:first-child
很难一目了然,但是将前者写为div *:first-child
会使区别显而易见.
Note: In some cases, adding a universal selector can make a selector easier to read, even though it has no effect on the matching behavior. For example,
div :first-child
anddiv:first-child
are somewhat difficult to tell apart at a quick glance, but writing the former asdiv *:first-child
makes the difference obvious.
这篇关于>之间有功能上的区别吗? *:第一个孩子和> :第一个孩子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!