开发者

Jquery getting an attribute from a list

I have the following code

<ul>
<li>
    <a><img src="mysource" alt="my alt tag 1"/></a>
</li>
<li>
    <a><img src="mysource" alt="my alt tag 2"/></a>
</li>
<li>
    <a><img src="mysource" alt="my alt tag 3"/></a>
</li>
<li>
    <a><img src="mysource" alt="my alt tag 4"/></a>
</li>
</ul>

I'm trying to get the alt tag of the image. I have the position of the list item i'm trying to access saved in a variable 'currentPos' but I can't get the alt content. Any help would be great. This is what i've tried so far

altText = $("ul li").i开发者_StackOverflow社区ndex(currentPos).find('img').attr('alt');


Use eq instead of index:

var altText = $("ul li").eq(currentPos).find('img').attr('alt');

Here's a working example.

index returns a number indicating the index position of the element, whereas eq actually gets the element at the specified index.


try :

altText = $("ul li").eq(currentPos).find('img').attr('alt');


try

altText = $('ul li').eq(currentPos).find('img').attr('alt');

Where currentPos is a 0 based index. so the first item is 0

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜