prev();和next();用法说明Jquery [英] prev(); and next(); usage clarification Jquery
问题描述
我们有一系列的div,我们想为当前所选DIV的下一个和上一个兄弟分配不同的类.
we have a series of divs, and we would like to assign different classes to the next and previous siblings of the currently selected DIV.
这是我们的代码:
<div class="panel">DIV content 1</div>
<div class="panel current">DIV content 2</div> //THIS IS THE CURRENTLY SELECTED PANEL
<div class="panel">DIV content 3</div>
<div class="panel">DIV content 4</div>
<div class="panel">DIV content 5</div>
据我了解,以下JQuery代码将查找.current的同级项,并在下一个和上一个类中添加这些类.但是,由于它正在查找.current元素,因此找不到em. 如何引用.current面板并将addClass指向下一个和上一个.panel元素?
As I understood, the following JQuery code would look for siblings of .current and add the classes next and previous. But since it's looking for .current elements, it'll not find em. How can I refer to the .current panel and addClass to the next and previous .panel elements?
谢谢.
$('.current').prev().addClass('previous');
$('.current').next().addClass('next');
推荐答案
根据您的需要,您可以使用 siblings()
, nextUntil
, nextAll
, prevAll
函数. next()
和prev()
分别将您带到上一个或下一个元素.
As per your needs, you may use siblings()
, nextUntil
, prevUntil
, nextAll
, prevAll
functions. The next()
and prev()
will get you to just one previous or next element respectively.
//To select all previous elements of `.current` element:
$('.current').prevAll('.panel').addClass('previous');
//To select all next elements of `.current` element:
$('.current').nextAll('.panel').addClass('next');
这篇关于prev();和next();用法说明Jquery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!