码迷,mamicode.com
首页 > 其他好文 > 详细

点击按钮显示隐藏层 和 切换按钮同时显示多个隐藏层

时间:2016-04-13 20:30:33      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:

按钮点击显示隐藏层(再次点击按钮则隐藏层关闭):

HTML部分:

<button type="button" id="show" onclick="showHidden()">点我显示隐藏层</button>

<div id="hidden" style="display:none">我是隐藏层。</div>

JS部分:

<script type=‘text/javascript‘>
function showHidden() {
var dis = document.getElementById(‘hidden‘);
if (dis.style.display == ‘none‘) {
document.getElementById(‘show‘).innerHTML = "再点我关闭隐藏层一 ";
dis.style.display = "";
} else {
document.getElementById(‘show‘).innerHTML = "点我显示隐藏层一 ";
dis.style.display = "none";
}
}
</script>

按钮切换同时显示多个隐藏层(需要用到jQuery):

HTML部分:

<button type="button" onclick="showHidden(‘hidden‘)" checked>点我关闭隐藏层二和三</button>
<button type="button" onclick="showHidden(‘show‘)">点我打开隐藏层二和三</button>

<div class="row hiddenAndShow" style="display:none">我是隐藏层二</div>

<div class="row hiddenAndShow" style="display:none">我是隐藏层三</div>

JS部分:

<script>
function showHidden(hiddenAndShow) {
if (hiddenAndShow == "hidden") {
$(‘.hiddenAndShow‘).each(function () {

$(this).css(‘display‘, ‘none‘);

});
} else {
$(‘.hiddenAndShow‘).each(function () {
$(this).css(‘display‘, ‘block‘);
});
}
}
</script>

 

 

示例:

 

<!doctype html>
<html>
<header>
<meta charset="utf-8">
</header>

<body>

<div style="text-align:center">
<button type="button" id="show" onclick="showHidden()">点我显示隐藏层一</button>
<div id="hidden" style="display:none">
我是隐藏层一
</div>
</div>
<br>
<br>
<div style="text-align:center">
<button type="button" onclick="showHidden1(‘hidden‘)" checked>点我关闭隐藏层二和三</button>
<button type="button" onclick="showHidden1(‘show‘)">点我打开隐藏层二和三</button>

<div class="row hiddenAndShow" style="display:none">我是隐藏层二</div>
<div class="row hiddenAndShow" style="display:none">我是隐藏层三</div>
</div>

<script src="http://www.weidulove.com/js/jquery-2.0.3.min.js"></script>

<script type=‘text/javascript‘>
function showHidden() {
var dis = document.getElementById(‘hidden‘);
if (dis.style.display == ‘none‘) {
document.getElementById(‘show‘).innerHTML = "再点我关闭隐藏层一";
dis.style.display = "";
} else {
document.getElementById(‘show‘).innerHTML = "点我显示隐藏层一";
dis.style.display = "none";
}
}
</script>

<script>
function showHidden1(hiddenAndShow) {
if (hiddenAndShow == "hidden") {
$(‘.hiddenAndShow‘).each(function () {

$(this).css(‘display‘, ‘none‘);

});
} else {
$(‘.hiddenAndShow‘).each(function () {
$(this).css(‘display‘, ‘block‘);
});
}
}
</script>
</body>

</html>

点击按钮显示隐藏层 和 切换按钮同时显示多个隐藏层

标签:

原文地址:http://www.cnblogs.com/Man-Dream-Necessary/p/5388499.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!