开发者

Accessing second element from a div

开发者 https://www.devze.com 2023-04-05 11:44 出处:网络
I have the following code snippet <div class=\'container\'> <a href=\'\'> <img alt=\'\' class=\'image0\' src=\'images/Gallery/gallery-01.jpg\' title=\'info\'/>

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号