下面我是通过class获取元素
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
<style>
.bb {
background: pink;
}
</style>
</head>
<body>
<div class="aa">
<h1>hahah</h1>
</div>
<script>
window.onload = function() {
//通过类名获取元素
var a = document.getElementsByClassName('aa')[0]
//给元素设置样式
a.style.backgroundColor = 'pink'
}
</script>
</body>
</html>
//后面不带索引值控制台出现报错
var a = document.getElementsByClassName('aa')