bil*_*lly 2 html jquery button show hide
嗨我一直试图用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>
Run Code Online (Sandbox Code Playgroud)
试试这个
$(".buttons").click(function () {
var divname= this.value;
$("#"+divname).slideToggle().siblings().hide("slow");
});
Run Code Online (Sandbox Code Playgroud)