js获取dom元素的方法
1.通过id获取
document.getElementById(‘id’)
得到的是一个元素,没有找到返回null
2.通过标签名
document.getElementsByTagName(‘box’)
得到一个数组,没有返回空数组
3.通过类名
document.getElementsByClassName(‘animated’)
得到一个数组,没有返回空数组
当获取到数组时,给数组的每一项添加相同的方法时,需要使用循环给每一个项加上方法。
js获取dom元素的方法
1.通过id获取
document.getElementById(‘id’)
得到的是一个元素,没有找到返回null
2.通过标签名
document.getElementsByTagName(‘box’)
得到一个数组,没有返回空数组
3.通过类名
document.getElementsByClassName(‘animated’)
得到一个数组,没有返回空数组
当获取到数组时,给数组的每一项添加相同的方法时,需要使用循环给每一个项加上方法。