写入字符串属性值;省略值或传入 null、undefined 时移除属性。传入空字符串时保留该属性。 toggleAttribute(name, true) 保证属性存在,新增时写入空字符串,已有属性保留原值。需要写入或更新 aria-label 等字符串值时,使用此函数。
null
undefined
toggleAttribute(name, true)
aria-label
要更新的 DOM 元素
属性名
Optional
要写入的字符串,省略或传入 null、undefined 表示移除
设置属性时,属性名不符合 DOM 的命名规则
0.2.0
const element = document.createElement('div');setOrRemoveAttribute(element, 'aria-label', '阅读区');element.getAttribute('aria-label') // '阅读区'setOrRemoveAttribute(element, 'aria-label');element.hasAttribute('aria-label') // false Copy
const element = document.createElement('div');setOrRemoveAttribute(element, 'aria-label', '阅读区');element.getAttribute('aria-label') // '阅读区'setOrRemoveAttribute(element, 'aria-label');element.hasAttribute('aria-label') // false
写入字符串属性值;省略值或传入
null、undefined时移除属性。传入空字符串时保留该属性。toggleAttribute(name, true)保证属性存在,新增时写入空字符串,已有属性保留原值。需要写入或更新aria-label等字符串值时,使用此函数。