Is there a functional difference between gt; *:first-child and gt; :first-child?(gt; 之间是否存在功能差异?*:first-child 和 gt;:第一个孩子?)
问题描述
写一段代码,我注意到我在一个地方写了>: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 之间的潜在混淆:第一个孩子
.在这里,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.
在新规范中我们还可以阅读:
除非元素是无特征的,否则通用选择器的存在对元素是否匹配选择器没有影响.
Unless an element is featureless, the presence of a universal selector has no effect on whether the element matches the selector.
和
注意:在某些情况下,添加通用选择器可以使选择器更易于阅读,即使它对匹配行为没有影响.例如,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.
这篇关于> 之间是否存在功能差异?*:first-child 和 >:第一个孩子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:> 之间是否存在功能差异?*:first-child 和 &am


- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- 400或500级别的HTTP响应 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01