获取所有的元素节点

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Document</title>
    7. </head>
    8. <body>
    9. <ul id="app">
    10. <li>hello world</li>
    11. <li>hello world</li>
    12. <li>hello world</li>
    13. <li>hello world</li>
    14. </ul>
    15. <script>
    16. // 获取所有的元素节点 下课自己写一遍
    17. var app = document.getElementById("app");
    18. var childs = app.childNodes;
    19. var arr = [];
    20. for(var i=0;i<childs.length;i++){
    21. if(childs[i].nodeType == 1){
    22. arr.push(childs[i])
    23. }
    24. }
    25. console.log(arr);
    26. </script>
    27. </body>
    28. </html>