Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
prev();下一步();使用说明Jquery_Jquery_Addclass_Siblings - Fatal编程技术网

prev();下一步();使用说明Jquery

prev();下一步();使用说明Jquery,jquery,addclass,siblings,Jquery,Addclass,Siblings,我们有一系列的DIV,我们希望为当前所选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,我们希望为当前所选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>

根据您的需要,您可以使用、、功能。
next()
prev()
将分别让您只访问上一个或下一个元素

//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');
你做得对 只需添加以下内容

$('.current').prev(".panel").addClass('previous');
$('.current').next(".panel").addClass('next');
$('.current').prev()在前面提供一个元素。current:

<div class="panel">DIV content 1</div>
<div class="panel">DIV content 3</div>
请参见演示:

如果要获取所有上一个或下一个元素,请使用()和()

请参见演示:

尝试以下操作:

$('.current').prevUntil('.panel:first').addClass('previous');
$('.current').nextUntil('.panel:last').addClass('next');

或:


不需要。我只需要直接的下一个和上一个同级…事实上,我不知道如何选择从.current开始的下一个和上一个.panel元素(该元素与其他元素具有相同的.panel类)
$('.current').prevUntil('.panel:first').addClass('previous');
$('.current').nextUntil('.panel:last').addClass('next');
$('.current').prevAll().addClass('previous');
$('.current').nextAll().addClass('next');