How to get a specific jQuery item from a list of items?

后端 未结 5 1176
难免孤独
难免孤独 2021-01-07 23:23

I have this:

  • first
  • second
  • third
  • fourth
相关标签:
5条回答
  • 2021-01-08 00:05

    I would try:

    $("ul li:nth-child(2)")
    
    0 讨论(0)
  • 2021-01-08 00:06

    The get method returns the DOM element, so then you would have to wrap it inside a new jQuery object.

    You can use the eq method:

    var j = $('ul li').eq(1); // gets the second list item
    
    0 讨论(0)
  • 2021-01-08 00:19

    Use :eq() Selector. For for example, for second element use:

     $("ul li:eq(1)"); 
    
    0 讨论(0)
  • 2021-01-08 00:20

    you can use nth-child

    $("ul li:nth-child(2)") //this will select second child because it is 1 based index
    

    here is a fiddle http://jsfiddle.net/xyyWh/

    0 讨论(0)
  • 2021-01-08 00:21

    $('li').get(0) will return plain DOM element. you cannot call jQuery methods on same.

    0 讨论(0)
提交回复
热议问题