开发者

jquery: possible to select an item only if it's followed by an item with a specific class?

开发者 https://www.devze.com 2022-12-27 10:11 出处:网络
the problem: I want to use jquery to add a class of \'warm\' to any li\'s neighboring to a li.hot <ul>

the problem:

I want to use jquery to add a class of 'warm' to any li's neighboring to a li.hot

<ul>
    <li></li> * ? (unknown amount of li's)
    <li></li>
    <li class="hot"></li>
    <li></li>
    <li></l开发者_如何学JAVAi> * ? (unknown amount of li's)
</ul>


If by "neighboring", you meant the previous and next elements then you can use .prev() and .next() selectors.

var liHot = $("li.hot");
liHot.prev("li").addClass("warm");
liHot.next("li").addClass("warm");


$('li.hot').prev().addClass('warm');
$('li.hot').next().addClass('warm');

I know it's basic, but you get the idea.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号