js 是怎么获取 html 元素的?
js 通过 dom 选择器获取 Html 元素
- 语法 document.选择器
- getElmentById() 参数为字符串;通过 html id 属性的值 返回 html 元素对象
- getElmentsByClassName() 参数为字符串;通过 html class 属性的值 返回 html 元素对象【集合】
- 注意:集合不能使用 dom 对象下属性。例如 Cannot set property ‘className’ of undefined 该对象没有中没有这个属性,这个对象不是 dom 对象
- getElmentsByTagName() 参数为字符串;通过 html 元素名字 返回 html 元素对象【集合】
- querySelector() 参数;是 css 选择器字符串;返回选中的元素对象
- querySelectorAll() 参数 是 css 选择器字符串;返回选中的元素对象【集合】
注意:选择器返回的集合不是一个真正的数据;是伪数组。不能使用数组中 API