How to get a collection of divs with jquery?
I want to get the second img:
<form id="formElem" name="formElem" action="" meth开发者_运维知识库od="post">
<fieldset class="step">
<img src="1.jpg" >
</fieldset>
<fieldset class="step">
<img src="2.jpg" >
</fieldset>
<fieldset class="step">
<img src="3.jpg" >
</fieldset>
</form>
I tried doing something like:
var imgSRC = $("div[id ='step']").get(1).find('img').attr('src');
alert(imgSRC);
Got nothing....
Use it like this:
var imgSRC = $('#formElem').find('fieldset').eq(1).find('img').attr('src');
or...
var imgSRC = $('#formElem fieldset:eq(1) > img').attr('src');
While the former is more to type, it's faster. Since the latter invokes the Sizzle to query the element, it's less to type but also slower!
References: .find(), .eq()
精彩评论