sheng-tool
    Preparing search index...

    Function setOrRemoveAttribute

    • 写入字符串属性值;省略值或传入 null、undefined 时移除属性。传入空字符串时保留该属性。 toggleAttribute(name, true) 保证属性存在,新增时写入空字符串,已有属性保留原值。需要写入或更新 aria-label 等字符串值时,使用此函数。

      Parameters

      • element: Element

        要更新的 DOM 元素

      • name: string

        属性名

      • Optionalvalue: string | null

        要写入的字符串,省略或传入 null、undefined 表示移除

      Returns void

      设置属性时,属性名不符合 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