<html>
<head>
</head>
<body>
<h1>HTMLCollection length Property</h1>
<p>一个 p 元素。</p>
<p class="myclass">另一个 p 元素。</p>
<p class="myclass">第三个 P 元素。</p>
<p>点击按钮为所有class="myclass"的元素添加背景色:</p>
<button onclick="myFunction()">设置颜色</button>
<script>
function myFunction() {
var x, i;
x = document.getElementsByClassName("myclass");
for (i = 0; i < x.length; i++) {
x[i].style.backgroundColor = "red";
}
}
</script>
</body>
</html>