javascript – 使用next / prev导航多个div
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – 使用next / prev导航多个div,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2789字,纯文字阅读大概需要4分钟。
内容图文
所以基本上我的DIV看起来像这样.
<div id="group">
<div id="one">one</div>
<div style="display:none" id="two">two</div>
<div style="display:none" id="three">three</div>
<div style="display:none" id="four">four</div>
</div>
<div id="next">next</div>
<div style="display:none" id="prev">prev</div>
<div style="display:none" id="prev">SUBMIT</div>
这只是一个例子,我甚至可以有10或20个div.
在这个例子中,我想从一到四导航.当它到达结束时,它必须隐藏下一个按钮并显示提交按钮.当我导航回第一页时,它必须隐藏prev按钮
这是我到目前为止所尝试的:
$("#next").click(function () {
$("#prev").show();
$("#one").hide();
$("#one").addClass("current");
$(".current").next().addClass("current").show();
$(".current").prev().removeClass("current").hide();
});
$("#prev").click(function () {
$("#prev").show();
$("#one").hide();
$("#one").addClass("current");
$(".current").prev().addClass("current").show();
$(".current").next().removeClass("current").hide();
});
这适用于某些导航,因为它变得混乱.一些指导对我和其他人有帮助.
谢谢
JSFIDDLE:http://jsfiddle.net/aVJBY/450/
解决方法:
我看到你有一个答案,但我会建议一种更加结构化的方法,它重用一个代码路径:
http://jsfiddle.net/TrueBlueAussie/aVJBY/460/
function updateItems(delta)
{
var $items = $('#group').children();
var $current = $items.filter('.current');
var index = $current.index();
var newIndex = index+delta;
// Range check the new index
newIndex = (newIndex < 0) ? 0 : ((newIndex > $items.length) ? $items.length : newIndex);
if (newIndex != index){
$current.removeClass('current');
$current = $items.eq(newIndex).addClass('current');
// Hide/show the next/prev
$("#prev").toggle(!$current.is($items.first()));
$("#next").toggle(!$current.is($items.last()));
}
}
$("#next").click(function () {
updateItems(1);
});
$("#prev").click(function () {
updateItems(-1);
});
笔记:
>范围上限可以简化,但你明白了.
>您不需要初始内联样式,因为可以在CSS中完成.
>这不受内容的任何限制.在这里我添加了6个div:http://jsfiddle.net/TrueBlueAussie/aVJBY/463/
更新
由于我不喜欢页面中初始“状态”需要样式化的情况,这里是一个新版本,它也正确设置初始状态而没有任何初始样式(使用0 delta).我还删除了一个冗余var:
function updateItems(delta)
{
var $items = $('#group').children();
var $current = $items.filter('.current');
$current = $current.length ? $current : $items.first();
var index = $current.index() + delta;
// Range check the new index
index = (index < 0) ? 0 : ((index > $items.length) ? $items.length : index);
$current.removeClass('current');
$current = $items.eq(index).addClass('current');
// Hide/show the next/prev
$("#prev").toggle(!$current.is($items.first()));
$("#next").toggle(!$current.is($items.last()));
}
$("#next").click(function () {
updateItems(1);
});
$("#prev").click(function () {
updateItems(-1);
});
// Cause initial selection
updateItems(0);
JSFiddle:http://jsfiddle.net/TrueBlueAussie/aVJBY/468/
内容总结
以上是互联网集市为您收集整理的javascript – 使用next / prev导航多个div全部内容,希望文章能够帮你解决javascript – 使用next / prev导航多个div所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。