显示/隐藏多个div的Javascript [英] Show/Hide Multiple Divs Javascript
问题描述
寻找一个好的JavaScript来帮助我用按钮隐藏/显示多个div单击不是一个href点击,因此我可以在博客中使用它。
我一直在寻找答案,并且一直无法找到使用JavaScript和/或CSS的好答案。我有点新手,所以请忍受我。
以下是我的代码,但我想简化它,并使其工作,以便它将关闭div,当我再次单击适当的按钮。
css
< head>
< style>
#myDIV1 {
width:150px;
height:150px;
background-color:lightblue;
display:none;
}
#myDIV2 {
width:150px;
height:150px;
background-color:lightblue;
display:none;
}
#myDIV3 {
width:150px;
height:150px;
background-color:lightblue;
display:none;
}
#myDIV4 {
width:150px;
height:150px;
background-color:lightblue;
display:none;
}
< / style>
< / head>
我知道有一种更简单的方法,但这是我能找到的唯一方法,我希望它在大部分时间内完成
html
< ;主体>
< p>点击按钮可查看div。< / p>
< button onclick =myFunction1()>一个< /按钮>
< button onclick =myFunction2()>两个< / button>
< button onclick =myFunction3()>三< / button>
< button onclick =myFunction4()>四个< /按钮>
< div id =myDIV1>
这是div1元素。
< / div>
< div id =myDIV2>
这是div2元素。
< / div>
< div id =myDIV3>
这是div3元素。
< / div>
< div id =myDIV4>
这是div4元素。
< / div>
Javascript
< script>
function myFunction1(){
document.getElementById(myDIV1)。style.display =block;
document.getElementById(myDIV2)。style.display =none;
document.getElementById(myDIV3)。style.display =none;
document.getElementById(myDIV4)。style.display =none;
$ b函数myFunction2(){
document.getElementById(myDIV1)。style.display =none;
document.getElementById(myDIV2)。style.display =block;
document.getElementById(myDIV3)。style.display =none;
document.getElementById(myDIV4)。style.display =none;
function myFunction3(){
document.getElementById(myDIV1)。style.display =none;
document.getElementById(myDIV2)。style.display =none;
document.getElementById(myDIV3)。style.display =block;
document.getElementById(myDIV4)。style.display =none;
function myFunction4(){
document.getElementById(myDIV1)。style.display =none;
document.getElementById(myDIV2)。style.display =none;
document.getElementById(myDIV3)。style.display =none;
document.getElementById(myDIV4)。style.display =block;
}
< / script>
任何帮助都会预先感谢。
添加 css
和<$ c $在 html
文件中加入c> js 文件,例如 -
Looking for a good JavaScript to help me hide/show multiple divs with a button click not an a href click so I can use it in blogger.
I've been looking for an answer for a while now and have been unable to find a good one that uses JavaScript and/or CSS. I am a bit of a novice so bear with me.
Following is my code that works but I would like to simplify it and make it work so that it will close the div when I click the appropriate button again. css I know there is an easier way but this is the only way that I can find that works for what I want it to do for the most part html Javascript Any help would be appreciated thanks in advance. I would suggest to separate your code first - it would be then more clean and reusable - like myStyle.css, myScript.js, index.html Add the
这篇关于显示/隐藏多个div的Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!< link rel =stylesheettype =text / csshref =myStyle.css>
$ b src
- >表示文件的源路径。这里我假设我们所有的 css
, js
,'html'文件在同一个地方。
var divs = [Div1,Div2,Div3,Div4]; var visibleDivId = null;函数divVisibility(divId){if(visibleDivId === divId){visibleDivId = null; } else {visibleDivId = divId; } hideNonVisibleDivs(); } function hideNonVisibleDivs(){var i,divId,div; for(i = 0; i< divs.length; i ++){divId = divs [i]; div = document.getElementById(divId); if(visibleDivId === divId){div.style.display =block; } else {div.style.display =none; }}}
.buttons a {font-size:16px ;}。buttons a:hover {cursor:pointer; font-size:16px;}
< div class = main_div>< div class =buttons>< a href =#onclick =divVisibility('Div1');> Div1< / a> | < a href =#onclick =divVisibility('Div2');> Div2< / a> | < a href =#onclick =divVisibility('Div3');> Div3< / a> | < a href =#onclick =divVisibility('Div4');> Div4< / a>< / div>< div class =inner_div>< div id =Div1> ;我是Div One< / div>< div id =Div2style =display:none;>我是Div Two< / div>< div id =Div3style =display: none;>我是Div Three< / div>< div id =Div4style =display:none;>我是Div Four< / div>< / div>< / div> ;
<head>
<style>
#myDIV1 {
width: 150px;
height: 150px;
background-color: lightblue;
display: none;
}
#myDIV2 {
width: 150px;
height: 150px;
background-color: lightblue;
display: none;
}
#myDIV3 {
width: 150px;
height: 150px;
background-color: lightblue;
display: none;
}
#myDIV4 {
width: 150px;
height: 150px;
background-color: lightblue;
display: none;
}
</style>
</head>
<body>
<p>Click button to see div.</p>
<button onclick="myFunction1()">One</button>
<button onclick="myFunction2()">Two</button>
<button onclick="myFunction3()">Three</button>
<button onclick="myFunction4()">Four</button>
<div id="myDIV1">
This is the div1 element.
</div>
<div id="myDIV2">
This is the div2 element.
</div>
<div id="myDIV3">
This is the div3 element.
</div>
<div id="myDIV4">
This is the div4 element.
</div>
<script>
function myFunction1() {
document.getElementById("myDIV1").style.display = "block";
document.getElementById("myDIV2").style.display = "none";
document.getElementById("myDIV3").style.display = "none";
document.getElementById("myDIV4").style.display = "none";
}
function myFunction2() {
document.getElementById("myDIV1").style.display = "none";
document.getElementById("myDIV2").style.display = "block";
document.getElementById("myDIV3").style.display = "none";
document.getElementById("myDIV4").style.display = "none";
}
function myFunction3() {
document.getElementById("myDIV1").style.display = "none";
document.getElementById("myDIV2").style.display = "none";
document.getElementById("myDIV3").style.display = "block";
document.getElementById("myDIV4").style.display = "none";
}
function myFunction4() {
document.getElementById("myDIV1").style.display = "none";
document.getElementById("myDIV2").style.display = "none";
document.getElementById("myDIV3").style.display = "none";
document.getElementById("myDIV4").style.display = "block";
}
</script>
css
and js
file in the html
file like -<link rel="stylesheet" type="text/css" href="myStyle.css">
<script type="text/javascript" src="myScript.js"></script>
src
-> indicates the source path of the file. Here I assume that all our css
, js
, 'html' file in same place. var divs = ["Div1", "Div2", "Div3", "Div4"];
var visibleDivId = null;
function divVisibility(divId) {
if(visibleDivId === divId) {
visibleDivId = null;
} else {
visibleDivId = divId;
}
hideNonVisibleDivs();
}
function hideNonVisibleDivs() {
var i, divId, div;
for(i = 0; i < divs.length; i++) {
divId = divs[i];
div = document.getElementById(divId);
if(visibleDivId === divId) {
div.style.display = "block";
} else {
div.style.display = "none";
}
}
}
.buttons a {
font-size: 16px;
}
.buttons a:hover {
cursor:pointer;
font-size: 16px;
}
<div class="main_div">
<div class="buttons">
<a href="#" onclick="divVisibility('Div1');">Div1</a> |
<a href="#" onclick="divVisibility('Div2');">Div2</a> |
<a href="#" onclick="divVisibility('Div3');">Div3</a> |
<a href="#" onclick="divVisibility('Div4');">Div4</a>
</div>
<div class="inner_div">
<div id="Div1">I'm Div One</div>
<div id="Div2" style="display: none;">I'm Div Two</div>
<div id="Div3" style="display: none;">I'm Div Three</div>
<div id="Div4" style="display: none;">I'm Div Four</div>
</div>
</div>