根据class属性获取元素var target = document.getElementsByClassName(className);
使用例子:循环中多个div,只显示一个。每个div都有一个按钮与之对应。<button οnclick="javascript:showSubReplyDiv(i)"><div id="div"+i class="showSingleDiv">
按钮对应下面的函数:function showSubReplyDiv(index){ var target = document.getElementsByClassName(“showSingleDiv”); for ( var i = 0; i < target.length; i++) { target[i].style.display="none"; } var tar=document.getElementById("subReplyDiv"+index); tar.style.display="block"; }