开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜