[英]showing and hiding divs by clicking a button
I have four divs, the first is showing on load of the site. 我有四个div,第一个显示网站的负载。 I am currently hiding the other three on load with CSS (display: none)
我目前正在使用CSS隐藏其他三个负载(显示:无)
Within that first div that is showing, I have a button that on click, I want to hide the current div and then show the next div. 在显示的第一个div中,我有一个单击的按钮,我想隐藏当前的div,然后显示下一个div。
<div id="one">
<h3>Placeholder</h3>
<form>
<button id="next1" type="button" class="btn btn-lg btn-default">Next</button>
</form>
</div><!--Closes One-->
<div id="two">
<h3>Placeholder</h3>
<form>
<button id="next2" type="button" class="btn btn-lg btn-default">Next</button>
</form>
</div><!--Closes Two-->
<div id="three">
<h3>Placeholder</h3>
<form>
<button id="next3" type="button" class="btn btn-lg btn-default">Next</button>
</form>
</div><!--Closes Three-->
<div id="four">
<h3>Placeholder</h3>
</div><!--Closes Four-->
So im looking to create some Jquery to make this happen. 因此,我希望创建一些Jquery来实现这一目标。
Here's the janky code I have that doesnt really work at all... 这是我所拥有的简陋代码,根本无法正常工作...
$("#next1").click(function(){
$("#two").show();
$("#one").hide();
});
$("#next2").click(function(){
$("#three").show();
$("#two").hide();
});
$("#next3").click(function(){
$("#four").show();
$("#three").hide();
});
Forgot CSS, my apologies! 忘了CSS,我很抱歉!
#two, #three, #four {
display: none;
}
I assume I dont need to explicitly say to hide all three divs every time due to CSS already hiding two through four, and then altering one at a time through Jquery. 我想我不需要显式地每次都隐藏所有三个div,因为CSS已经隐藏了2到4,然后通过Jquery一次更改了一个。
Thank you ahead of time! 提前谢谢你!
This should do it: 应该这样做:
(you used classes instead of ids for your elements) (您在元素中使用了类而不是ID)
$("#next1").click(function(){ $("#two").show(); $("#one").hide(); }); $("#next2").click(function(){ $("#three").show(); $("#two").hide(); }); $("#next3").click(function(){ $("#four").show(); $("#three").hide(); });
#two, #three, #four { display: none; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="one"> <h3>Placeholder 1</h3> <form> <button id="next1" type="button" class="btn btn-lg btn-default">Next</button> </form> </div><!--Closes One--> <div id="two"> <h3>Placeholder 2</h3> <form> <button id="next2" type="button" class="btn btn-lg btn-default">Next</button> </form> </div><!--Closes Two--> <div id="three"> <h3>Placeholder 3</h3> <form> <button id="next3" type="button" class="btn btn-lg btn-default">Next</button> </form> </div><!--Closes Three--> <div id="four"> <h3>Placeholder 4</h3> </div><!--Closes Four-->
So this is the simple query which you are looking after 这是您要查询的简单查询
$(".btn-default").click(function(){
$(this).closest('div').hide(); //Gets the closest div associated with your button click
$(this).closest('div').next('div').show(); //hide the next div
});
try this 尝试这个
$("button.btn-default").click(function(){ $(this).closest('div').hide(); $(this).closest('div').next('div').show(); });
#two, #three, #four { display: none; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="one"> <h3>Placeholder 1</h3> <form> <button id="next1" type="button" class="btn btn-lg btn-default">Next</button> </form> </div><!--Closes One--> <div id="two"> <h3>Placeholder 2</h3> <form> <button id="next2" type="button" class="btn btn-lg btn-default">Next</button> </form> </div><!--Closes Two--> <div id="three"> <h3>Placeholder 3</h3> <form> <button id="next3" type="button" class="btn btn-lg btn-default">Next</button> </form> </div><!--Closes Three--> <div id="four"> <h3>Placeholder 4</h3> </div><!--Closes Four-->
It's not necessary to give each div it's own class or ID. 不必为每个div分配自己的类或ID。
You could give them all the same class and use the jQuery eq: filter to choose the correct div. 您可以为它们提供相同的类,并使用jQuery eq:过滤器选择正确的div。
Example code: 示例代码:
$(".custom button").click(function(){
var nextDiv = $(this).attr('data-id'); // Usually a +1 for the next div, but since eq filter is zero based, it works in this case.
$('.custom').hide();
$('.custom:eq(' + nextDiv + ')').show();
});
Working example: https://jsfiddle.net/ore5h6tk/ 工作示例: https : //jsfiddle.net/ore5h6tk/
Another example, hiding all but the first with CSS: https://jsfiddle.net/ore5h6tk/1/ 另一个示例,使用CSS隐藏除了第一个以外的所有示例: https : //jsfiddle.net/ore5h6tk/1/
Hope this helps. 希望这可以帮助。
实际上,您的代码没有错。只需使四个div看起来有所不同即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.