如何选择区间之间的元素
我有 html,想从第 10 个位置选择间隔 5 个元素。如何做到这一点?
我的 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:
$(".chaire:gt(10):lt(15)");
但它选择了我的 div 和 img 标签。但我需要 div 标签。
I have html and want to select interval 5 elements from 10'th position. How to do this?
My 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>
I try with jquery:
$(".chaire:gt(10):lt(15)");
but it select me div and img tags. But I need div tags.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的代码可以很好地完成工作,并且仅选择
元素。看一下这个小提琴示例,它找到了 5 个
元素第 10 个之后 (
:gt(9):lt(15)
)。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/。示例代码的下一部分在结果中查找
元素,并将其 src 更改为其他内容。
您最好使用
slice
,这只是对结果因此不那么混乱,更不用说更快了:(示例)
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)
).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)
.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.You're better off using
slice
, which is only a single operation on the result and therefore less confusing, not to mention faster:(example)
试试这个:
这是一个可以玩的jFiddle。
http://jsfiddle.net/rcravens/m3j6K/
它看起来像是链接“gt”和“ lt' 选择器意味着 'lt' 应用于 'gt' 之后剩余的内容。
鲍勃
try this:
Here is a jFiddle to play around with.
http://jsfiddle.net/rcravens/m3j6K/
It looks like chaining the 'gt' and 'lt' selectors means 'lt' is applied to what remains after the 'gt'.
Bob