[英]Toggle 2 div with one onclick
I have 2 divs which i need to toggle them with one button, the first one slide Up and the second one slide down. 我有2个div,我需要用一个按钮切换它们,第一个向上滑动,第二个向下滑动。
HTML dasdasdasdasd HTML dasdasdasdasd
<button>Toggle 'em</button>
JQuery JQuery的
$( "button" ).click(function() {
$( "#nav" ).slideUp();
});
Can anybody help me out? 有人可以帮帮我吗?
You can use class
instead of id
and make one div
hidden by using display:none;
您可以使用
class
而不是id
并使用display:none;
隐藏一个div
display:none;
html: HTML:
<div class="nav" style="background:black; width:100%; height:95px">dasdasdasdasd</div>
<div class="nav" style="background:gray; height:200px;display:none;"></div>
<button>Toggle 'em</button>
jquery jQuery的
$( "button" ).click(function() {
$( ".nav" ).toggle( "slow" );
});
Use a class
or then name
attribute instead of an id
. 使用
class
或name
属性而不是id
。
HTML HTML
<div class="nav" style="background:black; width:100%; height:95px">dasdasdasdasd</div>
<div class="nav" style="background:gray; height:200px"></div>
<button>Toggle 'em</button>
JS JS
$( "button" ).click(function() {
$( ".nav" ).toggle( "slow" );
});
PS PS
Never use the same id
for two elements. 永远不要对两个元素使用相同的
id
。 An id
should be unigue. id
应该是unigue。
Duplicate ids are invalid html, you should rather use class instead. 重复的id是无效的html,你应该改用class。
You will also need to hide one element in beginning for toggling.: 您还需要在开始时隐藏一个元素以进行切换:
$( "button" ).click(function() {
$( ".nav" ).slideToggle( "slow" );
});
It seems that your div
has an id of nav
. 看来你的
div
有nav
ID。 In order to select 2 divs, you can try assigning the same class name to both, for example: <div class="nav"></div>
(x2) 为了选择2个div,您可以尝试为两者分配相同的类名,例如:
<div class="nav"></div>
(x2)
Then change your jQuery to: $('.nav').slideUp();
然后将你的jQuery更改为:
$('.nav').slideUp();
That way, jQuery knows to select both divs and slide them up. 这样,jQuery知道选择两个 div并将它们向上滑动。
Try this, 尝试这个,
Html HTML
<div id="nav1" style="background:black; width:100%; height:95px">dasdasdasdasd</div>
<div id="nav2" style="background:gray; height:200px"></div>
And some CSS 还有一些CSS
#nav1{
display:none;
}
Script 脚本
$( "button" ).click(function() {
$( "#nav1,#nav2" ).slideToggle( "slow" );
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.