我有一百万个 div
元素。
我想实现以下功能:
当我点击一个 vsibile
div
时,它会消失。当我点击一个
div
时,任何 不可见 的div
都会重新出现。
例子:
<div>1</div> <!-- Invisible div -->
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div> <!-- Currently visible. On click, div 1 becomes visible and this becomes invisible-->
... 1.000.000
最佳答案
prev = false;
document.querySelectorAll("#test div").forEach(function(a,i){
a.index = i;
a.addEventListener("click",function(){
if (prev !== false) document.querySelectorAll("#test div")[prev].style.opacity = 1;
this.style.opacity = 0;
prev = this.index;
})});
#test * {
width: 60px;
height: 60px;
margin: 10px;
background-color: green;
}
<div id="test">
<div></div>
<div></div>
<div></div>
<div></div>
</div>
关于javascript - 单击时切换 DIV 的可见性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39302266/