您可以使用document.getElementsByTagName("DIV");
function myFunction() {
var x = document.getElementsByTagName("DIV");
for(var i=0; i < x.length; i++)
{
if (x[i].style.display === 'none') {
x[i].style.display = 'block';
} else {
x[i].style.display = 'none';
}
}
}
.myDIV {
width: 25%;
padding: 50px 0;
text-align: center;
background-color: lightblue;
margin-top:20px;
margin-right:10px;
}
<body>
<p>Click the "Try it" button to toggle between hiding and showing the DIV element:</p>
<button onclick="myFunction()">Try it</button>
<div class="myDIV">
This is my DIV element.
</div>
<div class="myDIV">
This is my DIV element.
</div>
<div class="myDIV">
This is my DIV element.
</div>
</body>
來源
2017-06-22 12:00:58
Raj
爲什麼不自己嘗試它,如果它不工作,有一個問題回到這裏? – Cristina
這正是我在做的:) – Cynthia