iT邦幫忙

0

document.getElementsByClassName 用法

js

嘗試隱藏下面全部DIV

<div id="div1" class="opt" style="display: none">SHOW</div>
<div id="div2" class="opt" style="display: none">SHOW</div>
<div id="div3" class="opt" style="display: none">SHOW</div>

先在body 新增input

<input name="name" type="button" onClick="show();" value="顯示">
<input name="name" type="button" onClick="hiddens();" value="隱藏">
<div id="div1" class="opt" style="display: none">SHOW</div>
<div id="div2" class="opt" style="display: none">SHOW</div>
<div id="div3" class="opt" style="display: none">SHOW</div>

最後加上JS

var s = document.getElementsByClassName('opt');
function show(){
    // var s = document.getElementsByClassName('opt');
    for(var i = 0;i<s.length;i++){
    s[i].style.display="";
    }
};

function hiddens(){
    // var s = document.getElementsByClassName('opt');
    for(var i = 0;i<s.length;i++){
    s[i].style.display="none";
    }
};

尚未有邦友留言

立即登入留言