admin管理员组

文章数量:1201413

What is the difference between $('#div1 a')[0] and $('#div1 a').eq(0) for the following markup

<div id="div1">
<a href="#">click</a>
</div>.

Please Help.

What is the difference between $('#div1 a')[0] and $('#div1 a').eq(0) for the following markup

<div id="div1">
<a href="#">click</a>
</div>.

Please Help.

Share Improve this question edited Mar 14, 2013 at 21:27 Soenhay 4,0486 gold badges38 silver badges61 bronze badges asked Feb 15, 2011 at 7:56 Niraj ChoubeyNiraj Choubey 4,04018 gold badges60 silver badges94 bronze badges 4
  • In your markup, all three will return the same. (no difference) – bcm Commented Feb 15, 2011 at 8:07
  • Using the square brackets is a shorthand for using .get api.jquery.com/get – Martin Jespersen Commented Feb 15, 2011 at 8:16
  • @meo I get you... looking at your hide example... thanks for correcting – bcm Commented Feb 15, 2011 at 8:21
  • This question took a while to find until I changed my search string from "jquery, .eq[0] vs [0]" to "jquery, difference between .eq[0] and [0]" – Soenhay Commented Mar 14, 2013 at 21:24
Add a comment  | 

1 Answer 1

Reset to default 28
$('div1 a')[0]

returns a direct reference to a DOM element

$('div1 a').eq(0)

returns a JQuery object

http://jsfiddle.net/meo/DP8as/

This will not work:

$('div a')[0].hide()

this will;

$('div a').eq(0).hide()

本文标签: javascriptDifference between (39selector39)0(39selector39)eq(index) in jqueryStack Overflow