如何隐藏/显示一个div被点击一个按钮时? [英] How can I hide/show a div when a button is clicked?
问题描述
我有一个 DIV
包含一个寄存器向导,我需要隐藏/显示此 DIV
当按钮被点击。
我怎样才能做到这一点?
下面我向您展示了code。
感谢:)
< DIV ID =巫师级=swMain>
< UL>
<立GT;< A HREF =#STEP-1>
<标签类=STEPNUMBER> 1 LT; /标签>
< / A>< /李>
<立GT;< A HREF =#步-2>
<标签类=STEPNUMBER> 2'; /标签>
< / A>< /李>
<立GT;< A HREF =#步-3>
<标签类=STEPNUMBER>第3版; /标签>
< / A>< /李>
<立GT;< A HREF =#步-4>
<标签类=STEPNUMBER> 4℃; /标签>
< / A>< /李>
< / UL>
< DIV ID =STEP-1>
< H2类=StepTitle>&PERFIL LT; / H>
<表CELLSPACING =3的cellpadding =3ALIGN =中心>
&所述; TR>
&所述; TD对齐=中心列跨度=3>&放大器; NBSP;&下; / TD>
< / TR>
&所述; TR>
< TD ALIGN =右>用户名:其中; / TD>
< TD align =left>
<输入类型=文本ID =用户名NAME =用户名价值=级=txtBox>
< / TD>
< TD align =left><跨度ID =msg_username>< / SPAN>&安培; NBSP;< / TD>
< / TR>
&所述; TR>
< TD ALIGN =右>密码:LT; / TD>
< TD align =left>
<输入类型=密码ID =密码NAME =密码值=级=txtBox>
< / TD>
< TD align =left><跨度ID =msg_password>< / SPAN>&安培; NBSP;< / TD>
< / TR>
< /表>
< / DIV>
使用jQuery。您需要设置,在您的按钮的click事件,这将触发你的向导div的知名度。
$('#BTN')。点击(函数(){
$('#向导)切换()。
});
参阅 JQuery的网站了解更多信息。
这也可以不JQuery的完成。仅使用标准的JavaScript:
<脚本类型=文/ JavaScript的>
功能toggle_visibility(ID){
变种E =的document.getElementById(id)的;
如果(e.style.display =='块')
e.style.display =无;
其他
e.style.display =块;
}
< / SCRIPT>
然后加入的onclick =toggle_visibility('id_of_element_to_toggle');
来用于显示和隐藏DIV的按钮
I have a div
that contains a register wizard, and I need hide/show this div
when a button is clicked.
How can I do this?
Below I show you the code.
Thanks :)
<div id="wizard" class="swMain">
<ul>
<li><a href="#step-1">
<label class="stepNumber">1</label>
</a></li>
<li><a href="#step-2">
<label class="stepNumber">2</label>
</a></li>
<li><a href="#step-3">
<label class="stepNumber">3</label>
</a></li>
<li><a href="#step-4">
<label class="stepNumber">4</label>
</a></li>
</ul>
<div id="step-1">
<h2 class="StepTitle">Perfil</h2>
<table cellspacing="3" cellpadding="3" align="center">
<tr>
<td align="center" colspan="3"> </td>
</tr>
<tr>
<td align="right">Username :</td>
<td align="left">
<input type="text" id="username" name="username" value="" class="txtBox">
</td>
<td align="left"><span id="msg_username"></span> </td>
</tr>
<tr>
<td align="right">Password :</td>
<td align="left">
<input type="password" id="password" name="password" value="" class="txtBox">
</td>
<td align="left"><span id="msg_password"></span> </td>
</tr>
</table>
</div>
Use JQuery. You need to set-up a click event on your button which will toggle the visibility of your wizard div.
$('#btn').click(function() {
$('#wizard').toggle();
});
Refer to the JQuery website for more information.
This can also be done without JQuery. Using only standard JavaScript:
<script type="text/javascript">
function toggle_visibility(id) {
var e = document.getElementById(id);
if(e.style.display == 'block')
e.style.display = 'none';
else
e.style.display = 'block';
}
</script>
Then add onclick="toggle_visibility('id_of_element_to_toggle');"
to the button that is used to show and hide the div.
这篇关于如何隐藏/显示一个div被点击一个按钮时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!