code:
<img src="image1.jpg" alt="This is 1 test.">
<img src="image2.jpg" alt="This is 2 test">
jquery code:
alert($('img')[0].attr('alt'));
why there is not pop up a box, and shows This is 1 test.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
To directly answer your question:
It doesn’t work because
[0]returns a native DOM Element of the selector, which doesn’t have a method called.attr(). You need to use.eq(index), which basically extracts out theindex‘th element of the elements represented by$(). Note that$()returns an array-like object and not an array per-se (thus[0]doesn’t work out of the box)