admin管理员组文章数量:1435859
Is there a way to select a element by index with Javascript or jQuery? For instance:
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
Under jQuery I could get the length as 4, now is there a way to select and then manipulate a element of "div.item" based on its index number?
Is there a way to select a element by index with Javascript or jQuery? For instance:
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
Under jQuery I could get the length as 4, now is there a way to select and then manipulate a element of "div.item" based on its index number?
Share Improve this question asked Jul 22, 2010 at 17:22 GeeGee 131 silver badge3 bronze badges4 Answers
Reset to default 5http://api.jquery./eq/
$("div.item").eq(0)
will give you the first element.
You could use the .eq()
selector for this job:
var element = $('div.item:eq(3)');
$('div.item:eq(4)');
this code will select the 4th div with .item class
$('div.item:eq(3)') or $("div.item").eq(3) or $("div.item").get(3)
If you are talking about getting the "element", the third option returns the DOM element, versus the first two that return the jQuery object containing the DOM element.
The first two are very similar. The difference is that the first one puts "eq" inside the query so you can only manipulate that single element. If you use the second one, you can do something like $("div.item").css('background','red').eq(3).css('background','blue') which cannot be done so easily using the other two methods.
本文标签: javascriptSelect element by index (multiple elements of same class)Stack Overflow
版权声明:本文标题:javascript - Select element by index (multiple elements of same class) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745665890a2669285.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论