gpt4 book ai didi

jquery - 在 jQuery 中测试下一个元素

转载 作者:行者123 更新时间:2023-12-01 02:03:44 24 4
gpt4 key购买 nike

如何测试下一个元素是否是某种类型 - 例如我想测试图像后面的下一个元素是否也是图像。如果是这样,那么我想向该元素添加一个类

例如如果是这种情况,那么它将触发 true 并向第一个图像添加一个类...

<p></p>
<img />
<img />
<p></p>

但是,在这种情况下,它将触发 false,并且什么也不会发生......

<p></p>
<img />
<p></p>

最佳答案

您可以使用.next() [docs].filter() [docs] 一起:

$('img').filter(function() {
return $(this).next('img').length === 1;
}).addClass('someClass');

您还可以使用next adjacent selector [docs].prev() [docs] :

$('img + img').prev().addClass('someClass');

关于jquery - 在 jQuery 中测试下一个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9212199/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com