getElementsByclassName
通过classu获取到一组元素,获取到的是一个类数组;
* 主语
* document 从整个文档中去获取class元素
* 父级 从父级去获取class元素
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> <script> window.onload=function(){ var greens=document.getElementsByClassName("green"); console.dir(greens); var color=document.getElementById('color'); var greens1=document.getElementsByClassName('green'); console.log(greens1); } </script> </head> <body> <ul id="color"> <li>red</li> <li class="green">green</li> <li>blue</li> <li>yellow</li> <li>pink</li> </ul> <ul> <li>red</li> <li class="green">green</li> <li>blue</li> <li>yellow</li> <li>pink</li> </ul> </body> </html>