如何使用 jquery 制作显示/隐藏切换按钮
您好,我一直在尝试使用 jquery 制作一个显示/隐藏切换按钮,但我只能找到显示 div 的按钮,但没有隐藏它的按钮,
下面是我发现的显示 div 但不允许您隐藏的示例之一除非您单击另一个按钮。我希望能够使用相同的按钮来显示和隐藏 div。我的页面上有多个 div,并且希望能够使用 jquery 而不是 javascript。
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function(){
$(".buttons").click(function () {
var divname= this.value;
$("#"+divname).show("slow").siblings().hide("slow");
});
});
</script>
</head>
<body>
<div id="buttonsDiv">
<input type="button" id="button1" class="buttons" value="div1"></input>
<input type="button" id="button2" class="buttons" value="div2"></input>
<input type="button" id="button3" class="buttons" value="div3"></input>
<input type="button" id="button4" class="buttons" value="div4"></input>
</div>
<div>
<div id="div1" style="display:none">
Hello World
</div>
<div id="div2" style="display:none">
Test
</div>
<div id="div3" style="display:none">
Another Test
</div>
<div id="div4" style="display:none">
Final Test
</div>
</div>
</body>
</html>
Hi I have been trying to make a show/hide toggle button with jquery but I can only find buttons that show a div but none that hide it
below is one of the examples I found that shows a div but it doesn't let you hide it unless you click on another button. I would like to be able to use the same button to show and hide a div. I have multiple divs on my page and would like to be able to use jquery instead of javascript.
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function(){
$(".buttons").click(function () {
var divname= this.value;
$("#"+divname).show("slow").siblings().hide("slow");
});
});
</script>
</head>
<body>
<div id="buttonsDiv">
<input type="button" id="button1" class="buttons" value="div1"></input>
<input type="button" id="button2" class="buttons" value="div2"></input>
<input type="button" id="button3" class="buttons" value="div3"></input>
<input type="button" id="button4" class="buttons" value="div4"></input>
</div>
<div>
<div id="div1" style="display:none">
Hello World
</div>
<div id="div2" style="display:none">
Test
</div>
<div id="div3" style="display:none">
Another Test
</div>
<div id="div4" style="display:none">
Final Test
</div>
</div>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
试试这个
Try this
更简单的是使用控件自己的切换功能。
Even simpler use control's own toggle function.
这是一个非常简单的脚本:
每次单击按钮都会切换另一个元素的可见性。
That's a pretty simple script to make:
Each click on the button would then toggle the visibility of the other element.
演示
应该可以了。
编辑:
切换的功劳归@AtoMerZ。他先拥有了它。
Demo
That should do it.
EDIT:
credit for toggle goes to @AtoMerZ. He had it first.
用这个切换你的脚本:
Switch your script with this:
jQuery .toggle() 方法应该可以帮助您:
来源: http://api.jquery.com/toggle/< /a>
The jQuery .toggle() method should help you:
Source: http://api.jquery.com/toggle/
您可以使用
toggle
方法来显示和隐藏元素。这将分别显示/隐藏每个 div:You can use the
toggle
method for showing and hiding an element. This will show/hide each div individually:假设您希望在显示新的
div
时隐藏所有其他div
:JS Fiddle 演示。
Assuming that you want all the other
div
s to be hidden when the new one is shown:JS Fiddle demo.