如何选择间隔之间的元素 [英] How select elements between interval
问题描述
我有html,想要从第10个位置选择间隔5个元素.该怎么做?
I have html and want to select interval 5 elements from 10'th position. How to do this?
我的html:
<div class="chaire">
<img alt="" src="2.gif">
</div>
<div class="chaire">
<img alt="" src="2.gif">
</div>
<div class="chaire">
<img alt="" src="2.gif">
</div>
<div class="chaire">
<img alt="" src="2.gif">
</div>
...
<div class="chaire">
<img alt="" src="2.gif">
</div>
我尝试使用jquery:
I try with jquery:
$(".chaire:gt(10):lt(15)");
但是它选择了div和img标签.但是我需要div标签.
but it select me div and img tags. But I need div tags.
推荐答案
您的代码可以很好地完成工作,并且仅选择 <div>
元素.看看这个示例小提琴,它在第10个(:gt(9):lt(15)
)之后找到5个<div>
元素.
Your code does the job just fine, and selects only <div>
elements. Take a look at this example fiddle, which finds 5 <div>
elements after the 10th (:gt(9):lt(15)
).
您拥有的选择器将仅选择具有该类名称的元素,在示例HTML的情况下,它只是
<div>
元素.不会选择<img>
元素.正如 rcravens 所指出的那样,这两个选择器分别修改了结果,因此您需要先使用:lt()
-:lt(15):gt(9)
.
The selector you have will only select elements with that class name, in the case of your example HTML, it's just the
<div>
elements. No <img>
elements will be selected. As rcravens had already pointed out, the two selectors modify the result separately, so you need to use :lt()
first - :lt(15):gt(9)
.
更新示例: http://jsfiddle.net/teQkf/3/.示例代码的下一部分在结果中找到<img>
元素,并将其src更改为其他内容.
Updated example at: http://jsfiddle.net/teQkf/3/. The next part of the example code finds the <img>
elements within the result and changes their src to something else.
最好使用 slice
,它只是对结果的一个操作因此不那么令人困惑,更不用说更快了:
You're better off using slice
, which is only a single operation on the result and therefore less confusing, not to mention faster:
$(".chaire").slice(10,15);
这篇关于如何选择间隔之间的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!