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.
精彩评论