JS getElementsByClassName()不适用于具有相同类的多个元素

时间:2018-05-09 14:25:31

标签: javascript html css

问题:这不起作用!!

HTML

<div id="circle1" class="circle"></div>
<div id="circle2" class="circle"></div>
<div id="circle3" class="circle"></div> 

JS

var circle = document.getElementsByClassName('circle');
        circle.onmouseover = function(){  /* This is not working */
           document.write("Hovering over one of the element that contains circle class");
        }

1 个答案:

答案 0 :(得分:-2)

var circle = document.getElementsByClassName("circle");
for (i = 0; i < circle.length; i++) {
    circle[i].onmouseover = function(){  /* This is not working */
           document.write("Hovering over one of the element that contains circle class");
        }
}

https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_document_getelementsbyclassname_loop

相关问题