DOM
元素节点
获取元素
Document.getElementsByClassName()
原文链接
https://developer.mozilla.org/zh-CN/docs/Web/API/Document/getElementsByClassName
描述
返回一个包含了所有指定类名的子元素的类数组对象。当在document对象上调用时,会搜索整个DOM文档,包含根节点。你也可以在任意元素上调用getElementsByClassName() 方法,它将返回的是以当前元素为根节点,所有指定类名的子元素。
语法
var elements = document.getElementsByClassName(names); // or:
var elements = rootElement.getElementsByClassName(names);
elements 是一个实时集合,包含了找到的所有元素。
属性
names 是一个字符串,表示要匹配的类名列表;类名通过空格分隔
属性操作:
- removeAtrribute()
- setrribute()
- getAttibute()
- hasAtrribute()
方法名
removeAtrribute()
原文链接
https://developer.mozilla.org/zh-CN/docs/Web/API/Element/removeAttribute
语法
element.removeAttribute(attrName);
描述
removeAttribute() 从指定的元素中删除一个属性
参数
attrName:指定要从元素中删除的属性的名称的DOMString。如果指定的属性不存在,则返回removeAttribute(),不会生成错误。
返回值
IE 返回boolean类型值,其他返回undefined
方法名
setAtrribute()
原文链接
https://developer.mozilla.org/zh-CN/docs/Web/API/Element/setAttribute
语法
let attribute = element.setAttribute(name, value);
描述
setAttribute(name, value)设置指定元素上的某个属性值。如果 属性已经存在,则更新该值;否则,使用指定的名称和值添加一个新的属性。
参数
name:表示属性名称的字符串
value:属性的值/新值
返回值
undefined
方法
getAttibute()
原文链接
https://developer.mozilla.org/zh-CN/docs/Web/API/Element/getAttribute
语法
let attribute = element.getAttribute(attributeName);
描述
getAttribute(attributeName)返回元素上一个指定的属性值。如果指定的属性不存在,则返回 null 或 ""(空字符串)
attribute:是一个包含 attributeName 属性值的字符串。
参数
attributeName:是你想要获取的属性值的属性名称。
方法名
hasAtrribute()
原文链接
https://developer.mozilla.org/zh-CN/docs/Web/API/Element/hasAttribute
语法
var result = element.hasAttribute(attName);
描述
hasAttribute 返回一个布尔值,指示该元素是否包含有指定的属性(attribute)
参数
attName 是一个字符串,表示属性的名称。
返回值
result 为返回的布尔值:true 或 false