Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我最近一直在考虑优化我的编码,以使其更整洁、更紧凑,但我在路上遇到了一些小问题。
我不能 100% 确定何时应该使用.eq()over.get()或:nth-child().
.eq()
.get()
:nth-child()
使用其中一个对我有什么好处,例如,最适合说什么,重复发生的事件或多个功能或仅使用一个功能等。我可以在某些情况下阅读它们的使用events 和 have but sources 只告诉我这么多,甚至在某些情况下,有人可能会使用一个,而另一个来源会使用另一个,但做完全相同的事情。
这是我需要澄清的“灰色”区域。
.eq(n)
.get(n)
.eq(n)[0]
:nth-child()适用于更复杂的选择器,例如:nth-child(2n+1).
:nth-child(2n+1)
您可以使用它代替:eq()选择器字符串,但我倾向于远离它并使用它.eq()来使选择器更具可读性:
:eq()
$('ul.parent > li.child:nth-child(2)') $('ul.parent > li.child:eq(2)') $('ul.parent > li.child').eq(2)