Jquery:结合“each”选择课后的所有内容功能
我只是想选择类“.last”之后的每个元素。
HTML:
<body>
<div class="option"></div>
<div class="option"></div>
<div class="option"></div>
<div class="last"></div>
<div class="option"></div>
<div class="option"><img src="a.jpg"/>
<table>
<td><tr>1</tr></td>
<td><tr>2</tr></td>
</table>
<div class="option"></div>
</body>
Jquery:
$('.option').nextAll(".last").each(function (i) {
$(this).css('background-color','red');
});
我想要完成什么:
<body>
<div class="option"></div>
<div class="option"></div>
<div class="option"></div>
<div class="last"></div>
<div class="option" style="background-color:red;"></div>
<div class="option" style="background-color:red;"><img style="background-color:red;" src="a.jpg"/>
<table style="background-color:red;"><td style="background-color:red;"><tr style="background-color:red;">abc</tr></td>
<td style="background-color:red;"><tr style="background-color:red;">abc</tr></td>
</table>
</div>
<div class="option" style="background-color:red;"></div>
</body>
编辑:
我需要选择 .last 类下面的所有元素
I just simply want to select each element after the class ".last".
HTML:
<body>
<div class="option"></div>
<div class="option"></div>
<div class="option"></div>
<div class="last"></div>
<div class="option"></div>
<div class="option"><img src="a.jpg"/>
<table>
<td><tr>1</tr></td>
<td><tr>2</tr></td>
</table>
<div class="option"></div>
</body>
Jquery:
$('.option').nextAll(".last").each(function (i) {
$(this).css('background-color','red');
});
What I want to acomplish:
<body>
<div class="option"></div>
<div class="option"></div>
<div class="option"></div>
<div class="last"></div>
<div class="option" style="background-color:red;"></div>
<div class="option" style="background-color:red;"><img style="background-color:red;" src="a.jpg"/>
<table style="background-color:red;"><td style="background-color:red;"><tr style="background-color:red;">abc</tr></td>
<td style="background-color:red;"><tr style="background-color:red;">abc</tr></td>
</table>
</div>
<div class="option" style="background-color:red;"></div>
</body>
EDIT:
I need to select ALL elements below .last class
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您几乎已经拥有它:
或者更简洁地说:
http://api.jquery.com/nextAll/
You almost had it:
or more succinctly:
http://api.jquery.com/nextAll/
试试这个选择器: http://api.jquery.com/next-siblings-selector/
您应该能够为 class="last" 提供一个选择器,然后为之后的任何内容提供选择器:
Try this selector: http://api.jquery.com/next-siblings-selector/
You should be able to provide a selector for class="last" then a selector for anything after that:
考虑到您想要实现的目标,为什么需要
each
?Given what you want to accomplish, why do you need
each
?http://jsfiddle.net/PWc3q/2/
http://jsfiddle.net/PWc3q/2/