(这与JS识别,锚点使用等无关)
<div class="myclass">classed element</div> <div id="myid">ided element</div>
有没有人有这个数字?我已经读过CSS ID’更快’,但是多少钱?我会冒险猜测它可以忽略不计,但知道它会很有趣.
解决方法
ID在某些情况下更快,但不是全部
It’s a common belief that ID selectors are the fastest,but this comes with a big caveat: IDs are fastest CSS selector only if they’re the key selector. What’s that? Well,while you probably read selectors from left to right,browsers read them from right to left.
这里还有一个针对您的号码请求的性能测试:http://oli.jp/2011/ids/#table1
结论
ID的正确使用速度更快,但与类相比差别很小 – 不值得考虑.
It seems to me that there are no convincing reasons to use IDs in selectors for CSS styling¹,as CSS classes can do everything IDs can. Hopefully you’ll agree there are some good reasons not to. Think about it the next time you start a personal project or redesign your own site,and try adding a class (or ARIA landmark roles) for styling instead. Save IDs for fragment identifiers or JavaScript hooks