DOM

  1. // 如何获取DOM元素
  2. var app = document.getElementsByTagName("div");
  3. // app HTMLCollection(HTML的集合),不是数组 类数组对象,可以使用数组的属性,可以for循环
  4. console.log(Array.isArray(app))
  5. console.log(app.length)
  6. for(var i=0;i<app.length;i++){
  7. console.log(app[i])
  8. }

getElementsByClassName

  1. <!-- div.one*4{hello $} -->
  2. <div class="one">hello 1</div>
  3. <div class="one">hello 2</div>
  4. <div class="one">hello 3</div>
  5. <div class="one">hello 4</div>
  6. <script>
  7. var all = document.getElementsByClassName("one");
  8. console.log(all);
  9. </script>

getElementById

  1. <div id="app">
  2. app
  3. <div class="one">one1</div>
  4. <div class="one">one1</div>
  5. <div class="one">one1</div>
  6. </div>
  7. <script>
  8. var app = document.getElementById("app");
  9. console.log(app);
  10. // NodeList
  11. var all = document.querySelectorAll(".one");
  12. console.log(all)
  13. </script>