我正在尝试让这个脚本一步一步显示我的div。当前代码仅显示点击时的所有div。如何让它检测div并逐步显示.Ex:1st click =>展开第1步的div,第2步clikc =>为第2步扩展div。之后,如果div全部展开,则单击该按钮时隐藏所有div。
TQ
<div id="stepshow">
<h3>Step 1</h3>
</div>
<div id="stepshow">
<h3>Step 2</h3>
</div>
<button id="stepbtn"></button>
<script>
var qqcounter = 1;
var maxstep = 3;
$("#stepbtn").text("Show step " + qqcounter);
$("#stepshow").hide();
$("#stepbtn").click(function(){
qqcounter++;
if(qqcounter < maxstep)
{
$("#stepbtn").text("Show step " + qqcounter);
$("#stepshow").slideDown();
}
else
{
$("#stepbtn").text("Hide step");
}
});
</script>
答案 0 :(得分:3)
以下是如何显示/隐藏的示例:
<强> jQuery的:强>
$('#stepbtn').click( function() {
if ($('#step2').is(':visible')) {
$('#step3').show();
$('#stepbtn').hide();
} else if ($('#step1').is(':visible')) {
$('#step2').show();
}
});
$('#backbtn').click( function() {
if ($('#step3').is(':visible')) {
$('#step3').hide();
$('#stepbtn').show();
} else if ($('#step1').is(':visible')) {
$('#step1').hide();
}
});
<强> CSS:强>
#step2 {
display:none;
}
#step3 {
display:none;
}
#backbtn { display: none }
<强> HTML:强>
<div id="step1">
<h3>Step 1</h3>
</div>
<div id="step2">
<h3>Step 2</h3>
</div>
<div id="step3">
<h3>Step 3</h3>
</div>
<button id="stepbtn">Next Step</button>
<button id="backbtn">Go Back a Step</button>
更新:
这是另一个版本,它为您提供了更大的灵活性:
<强> jQuery的:强>
$(document).ready( function() {
var current_step = 1;
var max_number_of_steps = 6;
$('#stepbtn').click( function() {
var next_step = current_step + 1;
$('#step'+next_step).slideDown();
$('#backbtn').show();
current_step++; // increase the step we are on...
if (current_step == max_number_of_steps) {
$('#stepbtn').hide();
}
});
$('#backbtn').click( function() {
$('#step'+current_step).slideUp();// hide it first,
current_step--; // now update, so we know the correct step we are on
if (current_step == 1) {
$('#backbtn').hide();
}
if (current_step < max_number_of_steps) {
$('#stepbtn').show(); // show, if its been hidden...
}
});
});
<强> CSS:强>
#step2,#step3,#step4,#step5,#step6 {
display:none;
}
#backbtn { display: none }
<强> HTML:强>
<div id="step1">
<h3>Step 1</h3>
</div>
<div id="step2">
<h3>Step 2</h3>
</div>
<div id="step3">
<h3>Step 3</h3>
</div>
<div id="step4">
<h3>Step 4</h3>
</div>
<div id="step5">
<h3>Step 5</h3>
</div>
<div id="step6">
<h3>Step 6</h3>
</div>
<button id="stepbtn">Next Step</button>
<button id="backbtn">Go Back a Step</button>
答案 1 :(得分:1)
html
<div id="stepshow1" class="stepshow">
<h3>Step 1</h3>
</div>
<div id="stepshow2" class="stepshow">
<h3>Step 2</h3>
</div>
<div id="stepshow3" class="stepshow">
<h3>Step 3</h3>
</div>
<input type="button" id="stepbtn" />
脚本
var qqcounter = 0;
var maxstep = 4;
$("#stepbtn").text("Show step " + qqcounter);
$(".stepshow").hide();
$("#stepbtn").attr('value', 'Show step' + (qqcounter+1));
$("#stepbtn").click(function () {
console.log(qqcounter);
qqcounter++;
if (qqcounter < maxstep) {
$("#stepbtn").attr('value', 'Show step' + (qqcounter + 1));
$('#stepshow' + qqcounter).slideDown();
}
else {
$("#stepbtn").attr('value', 'Hide step');
$(".stepshow").hide();
qqcounter = 0;
}
});
答案 2 :(得分:0)
它不起作用,因为你有相同的ID“stepshow”你应该改为smth,例如“stepshow_1”:
$('btn').click(function(){
$('#stepshow_'+counter).show();
counter++;
});