<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="box">box</div>
<div class="top">top</div>
<div class="top">top</div>
<div class="top">top</div>
<div class="top">top</div>
<div class="top">top</div>
<ul>
<li>1</li>
<li>1</li>
<li>1</li>
<li>1</li>
</ul>
<script>
var box = document.getElementById('box')
box.innerHTML = 'box3333'
var divs = document.getElementsByClassName('top')
console.log(divs)
![获取所有class为top的所有元素](https://img-blog.csdnimg.cn/20200610224026365.png)
console.log(divs[0])
![这是通过下标取获取某一个元素](https://img-blog.csdnimg.cn/20200610224357135.png)
for (var i = 0; i < divs.length; i++) {
divs[i].innerHTML = 'top' + i
}
var list = document.getElementsByTagName('li')
console.log(list)
for (var i = 0; i < list.length; i++) {
list[i].style.background = 'red'
}
</script>
</body>
</html>
Will