通过jQuery逐个选择下一个元素
Select next element via class with jQuery one at a time
Backstory:我有一个搜索过短语的HTML,它在每个实例周围放置了一个跨度。如果我搜索Lorem,每个出现这个词的地方都是这样的
<span class="live-search-highlight">Lorem</span>
给它一点亮点。第一个实例被赋予一个不同的类,具有更明显的亮点,如下所示:
<span class="highlight-current">Lorem</span>
问题:我想点击一个按钮,从.highlight-current
开始,我想找到.live-search-highlight
的下一个实例,并将其类切换到.highlight-current
。如何用jQuery实现这一点?以下是目前为止我所做的,它只工作一次:
$(document.body).on('click','.button', function(){
// Find highlight-current, then get the next instance and make that one current
$('.highlight-current').parent().nextAll(".live-search-highlight").eq(0).removeClass('live-search-highlight').addClass('highlight-current');
// Reset the previous one so it's no longer current
$('.highlight-current:first').removeClass('highlight-current').addClass('live-search-highlight');
});
JsFiddle: http://jsfiddle.net/kthornbloom/g5skaek7/3/
创建所有突出显示元素的集合更简单。然后使用索引来确定当前/下一个位置。
当到达最后一个时,Following将恢复到开始
// create collection of all highlighted elements
var $highlights = $('.live-search-highlight'),
// isolate the current one from collection and remove the secondary class
$currHighlight = $highlights.filter('.highlight-current').removeClass('highlight-current'),
// use index of current to determine next
nextIndex = $highlights.index($currHighlight) + 1;
// revert to beginning if index is at the end
if(nextIndex === $highlights.length){
nextIndex = 0;
}
// add secondary class to next (or first) in collection
$highlights.eq(nextIndex).addClass('highlight-current');
我将保留主类,只添加/删除次要类
演示如果您不删除'live-search-highlight'类,而是增加您存储的索引,则会有所帮助。这允许您在文档中循环,并将使实现前进/后退控制变得容易。
var currentIndex = 0,
totalMatches = 0,
currentClass = 'highlight-current',
normalClass = 'live-search-highlight';
$(document.body).on('click','.button', function(){
var matches = $('.'+normalClass);
totalMatches = matches.length;
if(totalMatches === 0){
return;
}
var newIndex = (currentIndex === totalMatches - 1) ? 0 : currentIndex + 1;
matches.eq(currentIndex).removeClass(currentClass);
matches.eq(newIndex).addClass(currentClass);
currentIndex = newIndex;
});
检查小提琴:http://jsfiddle.net/e00x5pbd/1/
问题是,您的代码查找下一个.live-search-highlight
通过它的父的兄弟姐妹。这在第一次工作是因为第一个高亮的父级与第二个高亮处于同一级别。之后它就不能工作了,因为它会在body的兄弟中寻找高光。
body
h1
highlight1 -> parent = h1 (has highlight siblings)
highlight2 -> parent = body (doesn't have any siblings)
highlight3
你必须存储:
- 突出显示并遍历数组而不是树遍历,或者
- 当前高亮显示的索引
应该可以。
var item_count = 0
$('.button').click(function(){
$('.live-search-highlight').eq(item_count - 1).removeClass('highlight-current');
$('.live-search-highlight').eq(item_count).addClass('highlight-current');
item_count++
});
JSFiddle
相关文章:
- 滚动到容器中的下一个元素-几乎到了
- 使用CSS或js,使用动态选择器选择任意li的下一个元素
- jQuery转盘下一个元素没有滑入
- 获取锚点的下一个元素
- j查询 如何选择当前元素之后的下一个元素
- 如何在 JavaScript 中获取下一个元素和更新
- jQuery网站的键盘快捷键,焦点使用左键和右键转到上一个或下一个元素
- 如果上一个元素为空,则隐藏下一个元素
- 使用表时,Jquery Next()未正确突出显示下一个元素
- 如何将所有下一个元素包装到
- 获取活动元素之后的下一个元素
- jQuery-在每个复选框中获取下一个元素
- 找不到下一个元素
- 如何获取下一个元素's值-jquery
- 如何选择 $(this) 之后的下一个元素
- 专注于下一个元素
- 导入 serval WebComponent 时,上一个自定义元素被下一个元素覆盖
- 将一个类添加到所有下一个元素,直到该类的子元素为止
- 如何在javascript中获取数组的下一个元素
- 使用 jquery 将类添加到下一个元素