<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <ul class="ul"> <li class="red">列表项01</li> <li>列表项02</li> <li class="green">列表项03</li> <li>列表项04</li> <li class="yellow">列表项05</li> </ul> </body> <script> var red = document.getElementsByClassName('red'); // console.log(red); red[0].style.background = 'red' //改方法不仅可以在document对象上调用,也可以在元素上调用,一般是在父元素上调用 document.getElementsByClassName('ul').item(0) .getElementsByClassName('green').item(0) .style.background = 'green' //class支持多值 var yellow = document.getElementsByClassName('yellow')[0]; yellow.style.background = 'yellow'; yellow.style.fontSize = '1.5rem' </script> </html>