简体   繁体   English

单击按钮即可隐藏许多div

[英]Hide many divs with on button click

I have 6divs and i want on button click to hide five and show one, on other button click to hide five others and show one. 我有6divs,我想单击按钮以隐藏五个并显示一个,单击其他按钮以隐藏五个其他并显示一个。 I don't want to hide all them one by one. 我不想一一藏起来。 Is any way to hide all by ID in the same time: My html code: 有什么办法可以同时隐藏所有ID:我的html代码:

<div id="ad1" style="display: none;">Div content 1</div>
<div id="ad2" style="display: none;">Div content 2</div>
<div id="ad3" style="display: none;">Div content 3</div>
<div id="ad4" style="display: none;">Div content 4</div>
<div id="ad5" style="display: none;">Div content 5</div>
<br/>
<button id='button1'>click1</button>
<button id='button2'>click2</button>
<button id='button3'>click3</button>
<button id='button4'>click4</button>
<button id='button5'>click5</button>

I tried something like this: 我尝试过这样的事情:

$("#button1").click(function () {
    $("#ad1").show();
    $("#ad2 #ad3 #ad4 #ad5").hide();
});

$("#button2").click(function () {
    $("#ad2").show();
    $("#ad1 #ad3 #ad4 #ad5").hide();
});

My jsfiddle : DEMO 我的jsfiddle: 演示

Any help :) 任何帮助:)

Just separate the elements with , to select multiple elements: 只需使用分隔元素,即可选择多个元素:

$("#button1").click(function () {
    $("#ad1").show();
    $("#ad2, #ad3, #ad4, #ad5").hide();
});

$("#button2").click(function () {
    $("#ad2").show();
    $("#ad1, #ad3, #ad4, #ad5").hide();
});

Here is your updated FIDDLE 这是您更新的FIDDLE

 $("#button1").click(function () { visible = $(".ads:visible"); next = visible.next(".ads"); debugger; if(next.length > 0 ){ next.show(); }else{ $(".ads").first().show(); } visible.hide(); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="ads" id="ad1">Div content 1</div> <div class="ads" id="ad2" style="display: none;">Div content 2</div> <div class="ads" id="ad3" style="display: none;">Div content 3</div> <div class="ads" id="ad4" style="display: none;">Div content 4</div> <div class="ads" id="ad5" style="display: none;">Div content 5</div> <br/> <button id='button1'>click1</button> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM