Javascript DOM - 如何循環(huán)通過從getElementsByClassName返回的元素
我們想知道如何循環(huán)通過從getElementsByClassName返回的元素。
<!DOCTYPE html>
<html>
<body>
<div class="content">Something</div>
<div class="content">Something Else</div>
<div class="content">Something Else</div>
<script type='text/javascript'>
var html = "Something From JS";
var elements = document.getElementsByClassName("content");
for(var x=0; x < elements.length; x++)
{
elements[x].innerHTML=html;
}
</script>
</body>
</html>
The code above is rendered as follows: