getElementsByTagName的2种语法(document.和父元素.)
注意事项1:返回值是元素对象的集合(伪数组)
<!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> <ul> <li>花落知多少1</li> <li>花落知多少2</li> <li>花落知多少3</li> <li>花落知多少4</li> </ul> <ol id="ol"> <li>举头望明月</li> <li>举头望明月</li> <li>举头望明月</li> <li>举头望明月</li> </ol> <script> var lis = document.getElementsByTagName('li'); console.log(lis); console.log(lis[0]); var ol = document.getElementById('ol'); console.log(ol.getElementsByTagName('li')); </script> </body> </html>