是:last-child选择器性能不好吗? [英] Is the :last-child selector bad for performance?

查看:230
本文介绍了是:last-child选择器性能不好吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用:last-child 选择器很多次,主要是在列表中使用 border-bottom 我使用 border:none; 为最后一个孩子,或当使用边距。所以我的问题是,从性能角度来看,:last-child 选择器坏吗?

I use :last-child selector plenty of times, mostly when using border-bottom in a list where I use border: none; for the last child, or when using margins. So my question is, is the :last-child selector bad from a performance point of view?

听说它从CSS2规范中删除,因为使用:first-child 很容易让浏览器检测,但是检测:last-

Also I've heard that it was removed from the CSS2 specification because using :first-child is easy for the browser to detect, but for detecting :last-child it needs to loop back.

推荐答案

如果由于性能问题而从CSS2延迟,

If it was deferred from CSS2 for performance concerns, but reintroduced in Selectors 3, I suspect that it's because performance is no longer an issue as it previously was.

请记住,:last-child

Remember that :last-child is the definitive and only way to select the last child of a parent (besides :nth-last-child(1), obviously). If browser implementers no longer have performance concerns, neither should we as authors.

我可以想到的唯一令人信服的原因是覆盖边框样式 :first-child 而不是:last-child 是允许与IE7和IE8兼容。如果这提高了性能,让这是一个副作用。如果您不需要 IE7和IE8支持,那么您不应该感到被迫使用:first-child over :last-child 。即使浏览器的性能是绝对关键,你应该通过测试和基准测试正确的方式,而不是过早的优化。

The only compelling reason I can think of for overriding a border style with :first-child as opposed to :last-child is to allow compatibility with IE7 and IE8. If that boosts performance, let that be a side effect. If you don't need IE7 and IE8 support, then you shouldn't feel compelled to use :first-child over :last-child. Even if browser performance is absolutely critical, you should be addressing it the proper way by testing and benchmarking, not by premature optimization.

这篇关于是:last-child选择器性能不好吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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