1-1 getElementById() 通过id来获取
<div id="app"></div><div class="one">one1</div><div class="one">one2</div><div class="one">one3</div><script>var app = document.getElementById("app")// console.log(app);</script>
1-2 getElementsByTagName() 返回带有指定标签名的对象的集合
HTMLCollection(HTML集合),不是数组 类数组对象,可以使用数组的属性,可以for循环```javascripthello world
var app = document.getElementsByTagName(“div”)
// app HTMLCollection(HTML集合),不是数组 类数组对象,可以使用数组的属性,可以for循环 console.log(Array.isArray(app)); // false console.log(app.length); for(var i = 0;i<app.length;i++){ console.log(app[i]); }
<a name="Jz5OL"></a>## 1-3 getElementsByClassName()通过class来获取- **实例 点击某个div消失**```javascript<div class="one">hello 1</div><div class="one">hello 2</div><div class="one">hello 3</div><div class="one">hello 4</div><script>/*this --> 在事件中,谁执行事件,this就指谁*/var all = document.getElementsByClassName("one")for(var i=0;i<all.length;i++){// 不能对HTML Collection 执行点击事件,只能对具体元素执行事件all[i].onclick=function(){// console.log(this)this.style.display = "none";}}console.log(all)</script>
