[英]Toggle div tag on button click
I have many buttons with id b1
, b2
... and each has a div
tag after it with id q1
, q2
... I want to toggle the div
tag when the corresponding button is clicked. 我有一个ID许多按钮
b1
, b2
......,每个人都有一个div
标签后,id为q1
, q2
......我想切换div
点击相应的按钮,当标签。 Example: When b1
is clicked q1
should toggle. 示例:单击
b1
, q1
应该切换。 It is given here. 它在这里给出。 http://upscfever.com/upsc-fever/en/topper/en-toppers.html
http://upscfever.com/upsc-fever/zh-CN/topper/en-toppers.html
$(document).ready(function(){ $(".info").hide(); $("#b1").click(function(){ $("#q1").toggle(); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id="b1" type="button" class="btn btn-primary btn-block"></button> <div id="q1" class="info"></div>
But as i might have 100 of buttons, how can i do the same thing without adding click on each button? 但是由于我可能有100个按钮,如何在不增加每个按钮单击的情况下做同样的事情?
Using attribute startswith selector ^
, get the id
of button and get the index value from it first. 使用属性startswith选择器
^
,首先获取button的id
,然后从中获取索引值。
$("[id^='b']").click(function(){
var index = this.id.match(/\d+$/)[0];
$("#q" + index ).toggle();
});
Demo 演示版
$(document).ready(function() { $(".info").hide(); $("[id^='b']").click(function() { var index = this.id.match(/\\d+$/)[0]; $("#q" + index).toggle(); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id="b1" type="button" class="btn btn-primary btn-block">Button1</button> <div id="q1" class="info">Info1</div> <button id="b2" type="button" class="btn btn-primary btn-block">Button2</button> <div id="q2" class="info">Info2</div> <button id="b3" type="button" class="btn btn-primary btn-block">Button3</button> <div id="q3" class="info">Info3</div> <button id="b4" type="button" class="btn btn-primary btn-block">Button4</button> <div id="q4" class="info">Info4</div>
Here you go with a solution 在这里你有一个解决方案
$(".info").hide(); $("button").click(function(){ $(this).next('div.info').toggle(); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button type="button" class="btn btn-primary btn-block">Button 1</button> <div class="info">Div 1</div> <button type="button" class="btn btn-primary btn-block">Button 2</button> <div class="info">Div 2</div>
No need to targeting using id, you can do it using jQuery .next()
无需使用id进行定位,您可以使用
jQuery .next()
Hope this will help you. 希望这会帮助你。
You could change your selector from $("#b1")
to something like $(".btn")
, and then use the id
to determine which q
you want to open, like this: 您可以将选择器从
$("#b1")
更改$(".btn")
,然后使用id
确定要打开的q
,如下所示:
$(".button").click(function(){
if(this.id){
$("#q"+this.id.substring(1)).toggle();
}
});
This way, it doesn't matter how you arrange your HTML, as long as the ids match 这样,只要ID匹配,如何安排HTML都没关系
$(document).ready(function(){ $(".info").hide(); $(".btn").click(function(){ if(this.id){ $("#q"+this.id.substring(1)).toggle(); } }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id="b1" type="button" class="btn btn-primary btn-block">b1</button> <button id="b2" type="button" class="btn btn-primary btn-block">b2</button> <div id="q1" class="info">q1</div> <div id="q2" class="info">q2</div>
Here you have it on JSFiddle: https://jsfiddle.net/py8q340p/ 在这里,您可以在JSFiddle上找到它: https ://jsfiddle.net/py8q340p/
Use an extra variable state to check your state and for first time only your first div is toggled later on both the divs are toggled. 使用额外的变量状态来检查您的状态,并且第一次只在切换两个div之后才切换您的第一个div。
$(document).ready(function(){ var state = 0; $(".info").hide(); $("#b1").click(function(){ if(state ==0 ) { $('#q1').toggle(); state = 1; } else { $("#q1").toggle(); $('#q2').toggle(); } }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id="b1" type="button" class="btn btn-primary btn-block">Toggle Divs</button> <div id="q1" class="info">Hello div 1</div> <div id="q2" class="info">Hello div 2</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.