循环通过路段标记:在第二个路段后停止
Cycling Through Section tags: Stops After 2nd Section
我有一组节标记,我正试图使用jQuery:循环使用这些标记
<ul id="office-nav">
<li class="prev"><a href="">Previous</a></li>
<li>Next Location</li>
<li class="next"><a href="">Next</a></li>
</ul>
<article id="offices">
<section class="office1">
<h3>Office 1</h3>
</section><!--end office1-->
<section class="office2">
<h3>Office 2</h3>
</section><!--end office2-->
<section class="office3">
<h3>Office 3</h3>
</section><!--end office3-->
</article><!--end offices-->
CSS:
#offices section{display:none;width:300px;height:300px;}
#offices section.office1{display:block;background-color:red;}
#offices section.office2{background-color:yellow;}
#offices section.office3{background-color:orange;}
这是我的jQuery:
var visibleBox = $('#offices section:visible');
var nextToShow = $(visibleBox).next('section:hidden');
$("#office-nav li.next a").click(function(event) {
visibleBox.hide();
if (nextToShow.length > 0) {
nextToShow.show();
} else {
$('#offices section:hidden:first').show();
}
event.preventDefault();
});
对我来说,这个周期一直到.office2
,但没有超过这个周期。此外,我该如何重写反向函数以向后循环?
Fiddle here:http://jsfiddle.net/pwb6D/2/
如果只在加载时获得可见的section
元素,则需要更新click
事件处理程序中的visibleBox
和nextToShow
变量。
看跌:
var visibleBox = $('#offices section:visible');
var nextToShow = $(visibleBox).next('section:hidden');
在click
事件处理程序中,因此每次用户单击next
/prev
按钮时都会更新:
$("#office-nav li.next a").click(function(event) {
var visibleBox = $('#offices section:visible');
var nextToShow = $(visibleBox).next('section:hidden');
visibleBox.hide();
if (nextToShow.length > 0) {
nextToShow.show();
} else {
$('#offices section:hidden:first').show();
}
event.preventDefault();
});
下面是一个演示:http://jsfiddle.net/pwb6D/4/
更新
您可以通过缓存所有section
标签并使用计数变量来跟踪当前状态来优化代码:
//cache all the sections, how many sections there are, and set the current section to the first (zero based index)
var $all_sections = $('#offices').children('section'),
curr_section = 0,
section_count = $all_sections.length;
//bind click event handler to "next" button
$("#office-nav li.next a").click(function(event) {
//hide the section that was showing
$all_sections.eq(curr_section).hide();
//increment the curr_section variable
curr_section++;
//check to make sure there is still another hidden section to show, if not reset the curr_section variable to zero
if (curr_section >= section_count) {
curr_section = 0;
}
//show the next section
$all_sections.eq(curr_section).show();
//prevent the default click behavior of the "next" link
event.preventDefault();
});
下面是一个演示:http://jsfiddle.net/pwb6D/5/
更新
您可以使用相同的代码进行一些调整,为您的"prev"链接创建功能:
$("#office-nav .prev a").click(function(event) {
$all_sections.eq(curr_section).hide();
//increment down instead of up
curr_section--;
//check if the new curr_section value is less than zero (instead of larger than the number of sections)
if (curr_section < 0) {
//reset curr_section variable to the last section
curr_section = (section_count - 1);
}
$all_sections.eq(curr_section).show();
event.preventDefault();
});
您正在visibleBox
中存储第一个可见部分。这不会被新可见的部分所取代。请检查以下代码以修复此问题。
var Boxes = $('#offices');
$("#office-nav li.next a").click(function(event) {
var visibleBox = Boxes.find('section:visible'), // Find the visible section in article.
nextToShow = visibleBox.next(); // Get the next section to visible
visibleBox.hide();
if (nextToShow.length > 0) {
nextToShow.show();
} else {
$('#offices section:hidden:first').show();
}
event.preventDefault();
});
这是一个比当前结构更容易使用的结构,并且不依赖于全局变量。
http://jsfiddle.net/CQGRL/1
$('.office:first').addClass('active'); //show first office
$(".next a").click(function(event) {
//abort if this is the end of the list
if ($('.active').is('.office:last')) { return false; }
//find next office in list and make it active. then remove active class from current
$('.active').next('.office').addClass('active').end().removeClass('active');
return false;
});
$(".prev a").click(function(event) {
//abort if this is the end of the list
if ($('.active').is('.office:first')) { return false; }
//find previous office in list and make it active. then remove active class from current
$('.active').prev('.office').addClass('active').end().removeClass('active');
return false;
});
相关文章:
- 下拉选择可自动更改第二个下拉选择
- 为什么不'在JQuery中找到第二个css选择器的工作
- 当单击第一个李时,它显示内容一,当单击第二个李时,它显示内容二
- CORS-重定向到第二个GET正在接收的页面
- 正则表达式与数字中的第二个点匹配
- 如何在react js中移动第二个组件
- 使用Javascript,如何显示<tr>在另一<tr>悬停时,隐藏第二个<tr>
- 关闭第二个事件源上的第一个事件源's onopen方法
- D3从嵌套的JSON中绘制第二个圆环图
- IE11中的第二个调用取消了第一个Fetch API调用
- 第一个字第一个字符第二个字第二个字符,一直到数组结束
- 角度指令:指向第二个元素
- 时间选择器:如何通过更改第一个值来更改第二个值
- 我如何从字符串中选出第一个单词的第一个字母,然后再选出第二个单词
- 与网络界面相比,谷歌搜索API有时会重复第一个结果,有时会重复第二个结果
- 第二个存在条件不起作用
- 从第一个日期选择器定义第二个日期选择器的开始日期
- 如果未选择第一个下拉列表,则禁用第二个下拉列表
- Ember.js(v2.4.5)组件不是't错过第二个动作
- 标签的rggraph问题Above未出现在第二个图形创建中