我有以下jQuery代码,您也可以在JSfiddle 此处找到:
/* Code for buttons individually */
$(document).ready(function() {
$(".panel_button").on('click', function() {
$(".panel").hide();
var targetPanel = $(this).attr('data-target');
$(targetPanel).show();
$(this).toggleClass('active');
});
});
/* Code for previous and next buttons */
$(document).ready(function(){
$(".panel").each(function(e) {
if (e != 0)
$(this).hide();
});
$("#next").click(function(){
if ($(".panel:visible").next().length != 0)
$(".panel:visible").next().show().prev().hide();
else {
$(".panel:visible").hide();
$(".panel:first").show();
}
return false;
});
$("#prev").click(function(){
if ($(".panel:visible").prev().length != 0)
$(".panel:visible").prev().show().next().hide();
else {
$(".panel:visible").hide();
$(".panel:last").show();
}
return false;
});
});
body {
height: 500px;
}
.contents {
width: 100%;
height: 20%;
}
.buttons {
background-color: green;
float: left;
width: 100%;
}
.panel_button {
float: left;
width: 20%;
box-sizing: border-box;
border-style: solid;
border-width: 1px;
}
#panel1, #panel2, #panel3 {
background-color: blue;
float: left;
width: 100%;
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="contents">
<div id="panel1" class="panel">
<div class="content_01a">Here goes content1a</div>
<div class="content_01b">Here goes content1b</div>
</div>
<div id="panel2" class="panel">
<div class="content_02a">Here goes content2a</div>
<div class="content_02b">Here goes content2b</div>
<div class="content_02c">Here goes content2c</div>
</div>
<div id="panel3" class="panel">
<div class="content_03a">Here goes content3a</div>
<div class="content_03b">Here goes content3b</div>
</div>
</div>
<div class="buttons">
<div class="panel_button" data-target="#panel1"> PREVIOUS </div>
<div class="panel_button" data-target="#panel1"> Button_01 </div>
<div class="panel_button" data-target="#panel2"> Button_02 </div>
<div class="panel_button" data-target="#panel3"> Button_03 </div>
<div class="panel_button" data-target="#panel1"> NEXT </div>
</div>
正如您在上面的代码中看到的那样,我根据点击的内容显示/隐藏不同contents的内容button。button_01直到button_03单独工作完美。
现在,我想使用此处prev的代码添加一个和按钮,但我无法使其工作。您知道我需要在第二个代码中更改什么以使和按钮工作吗?nextjQueryprevnext