jQuery循环-从下一个元素继续 [英] jQuery loop - continue from next element
问题描述
功能是删除一个元素并继续循环.删除元素后,我正在使用div:FIRST或LAST继续操作.如何从下一个元素继续. 例如,删除注释3之后,我可以使其从Notes 1重新开始循环.但实际上,我希望它从Notes 4继续. 谢谢
The function is to remove an element and continue the loop. I'm using div:FIRST or LAST to continue after an element is removed. How to continue from the next element. For example, after removing notes 3, I can make it the loop to start again from Notes 1. But I actually want it to continue from Notes 4. Thanks
Style
#NotesBlock {
position: absolute;
width: 600px;
margin: 0px auto;
}
.divNotes {
display: none;
padding: 12px;
width: 100%;
text-align: center;
position: absolute;
}
<a href="#" class="CloseNotes">Not Interesting</a>
<div id="NotesBlock">
<div class="divNotes">
<div class=" NotesTopic">
<h4>Topic 1</h4>
<p>1st Topic description</p>
</div>
</div>
<div class="divNotes">
<div class=" NotesTopic">
<h4>Topic 2</h4>
<p>2nd Topic description</p>
</div>
</div>
<div class="divNotes">
<div class=" NotesTopic">
<h4>Topic 3</h4>
<p>3rd Topic description</p>
</div>
</div>
<div class="divNotes">
<div class=" NotesTopic">
<h4>Topic 4</h4>
<p>4th Topic description</p>
</div>
</div>
<div class="divNotes">
<div class=" NotesTopic">
<h4>Topic 5</h4>
<p>5th Topic description</p>
</div>
</div>
脚本
var notesElement = null;
function notesLoop(elem) {
notesElement = elem;
elem.fadeIn()
.delay(1500)
.fadeOut(function () {
notesLoop(elem.next());
if(elem.next().length === 0) {
notesLoop($(".divNotes:first"));
}
});
}
$(document).ready(function () {
notesLoop($(".divNotes:first"));
$(".CloseNotes").click(function (e) {
e.preventDefault();
if (notesElement)
$(notesElement).remove();
notesLoop($(".divNotes:first"));
});
});
更新:
我没有使用PUSH()或PUSHSTACK().我正在尝试使用它们更新此代码.是否有可能?我是否需要为此发布另一个问题?谢谢
I haven't used the PUSH() or PUSHSTACK(). I'm trying to update this code using them. Is it possible? Do I have to post another question for this? Thanks
我更新的代码:
推荐答案
您可以获取要删除的元素的索引,然后根据该索引发送选择器.
You can get the index of the element you are removing and then send the selector based on that index.
$(document).ready(function () {
notesLoop($(".divNotes:first"));
$(".CloseNotes").click(function (e) {
e.preventDefault();
var divIndex = notesElement.index();
if(divIndex == $('.divNotes').length -1){
divIndex = 0;
}
if (notesElement)
$(notesElement).remove();
notesLoop($(".divNotes:eq("+divIndex+")"));
});
});
这篇关于jQuery循环-从下一个元素继续的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!