Accessing second element from a div
I have the following code snippet
<div class='container'>
<a href=''>
<img alt='' class='image0' src='images/Gallery/gallery-01.jpg' title='info'/>
</a>
<a href=''>
<img alt='' class='image1' src='images/Gallery/gallery-01.jpg' title='info'/>
</a>
<a href=''>
<img alt='' class='image2' src='images/Gallery/gallery-01.jpg' title='info'/>
开发者_Python百科 </a>
</div>
I could use
$('.container a:first')
or $('.container a:last')
to access the first and the last elements, but how can i access the second anchor tag on the div? .
Use the :eq
selector:
$('.container a:eq(1)')
or (preferably) the .eq
function:
$('container a').eq(1)
Here you go:
$( '.container > a:eq(1)' )
Live demo: http://jsfiddle.net/rK4qc/
You can use:
$('.container a:nth-child(2)')
You could try $('.container a:first').next(); to get the second element.
$('.container a')[1]
will return the DOM element.
$($('.container a')[1])
will get you a Jquery object for the second object.
Check your result lengths appropriately.
精彩评论