.eq()和.get()和:nth-​​child()之间的区别? [英] Difference between .eq() and .get() and :nth-child()?

查看:105
本文介绍了.eq()和.get()和:nth-​​child()之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近一直在寻找优化我的编码,这样做比较干净,更干净,更紧凑,我在路上遇到了一些障碍。



我是不要100%确定当我应该使用一个函数,如 .eq()超过 .get():第n个孩子()



对于我来说,使用一个比较好的是什么,例如,最适合说,重复发生的事件或多个功能,或只是一个使用功能等。我可以在某些事件中阅读它们的使用,但是来源只会告诉我这么多,甚至在某些情况下有人可能使用一个,另一个来源将使用另一个,但要做同样的事情。 / p>

这是我需要澄清的灰色区域。

解决方案


  • .eq(n)检索n-1 th jQuery对象

  • .get(n)检索n-1 th DOM元素的。这就像做 .eq(n)[0]

  • :nth -child()用于更复杂的选择器,如:nth-​​child(2n + 1)



    您可以使用它代替选择器字符串中的:eq(),但我倾向于远离它并使用 .eq()使选择器更易读:

      $ 'ul.parent> li.child:nth-​​child(2)')
    $('ul.parent> li.child:eq(2)')
    $('ul.parent > li.child')。eq(2)



I have recently been looking at optimizing my coding so that is is neater and cleaner and more compact and i have encountered a little bump in the road.

I am not 100% sure when i should use a function like .eq() over .get() or :nth-child().

What would the benefits be for me to use one over the other for example, what one would be best for say, reoccurring events or, multiple functions, or just a single use function etc. I can read up on the use of them in certain events and have but sources only tell me so much and even in some cases someone may use one and the other source will use another but to do the exact same thing.

This is the 'gray' area that i need some clarification in please.

解决方案

  • .eq(n) retrieves the n-1th jQuery object.
  • .get(n) retrieves the n-1th DOM element. It'd be like doing .eq(n)[0].
  • :nth-child() is for more complex selectors like :nth-child(2n+1).

    You could use it in place of :eq() in selector strings, but I tend to stay away from it and use .eq() to make the selector more readable:

    $('ul.parent > li.child:nth-child(2)')
    $('ul.parent > li.child:eq(2)')
    $('ul.parent > li.child').eq(2)
    

这篇关于.eq()和.get()和:nth-​​child()之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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