浏览器与 Web API / 系统课时

使用 getAttribute 与 setAttribute

使用 getAttribute 与 setAttribute

本课将把元素属性操作整理成一套可直接使用的流程:读取前区分缺失值,修改时理解字符串转换,删除时使用专门方法,并通过布尔属性示例避免把 disabled="false" 误判为未禁用。

学习目标

  • 使用四个核心方法读取、设置、判断和删除属性
  • 正确区分属性缺失、空字符串与普通字符串
  • 按“是否存在”规则处理布尔属性

属性 API 的基本规则

属性方法直接作用于元素标签上的属性集合,适合处理 data-*、aria-*、自定义属性和需要精确属性名的场景。

方法 返回或行为 典型用途
getAttribute(name) 返回字符串;属性不存在时返回 null 读取原始属性值
setAttribute(name, value) 将值转换为字符串后写入 新增或覆盖属性
hasAttribute(name) 返回布尔值 判断属性是否存在
removeAttribute(name) 删除属性,无返回值 恢复为缺失状态

getAttribute 返回空字符串不等于属性缺失。例如 title="" 的结果是 "",而没有 title 时结果是 null。需要判断存在性时,优先调用 hasAttribute,不要只依赖真假转换。

主示例:完成属性生命周期

下面的完整页面依次读取、更新、判断和删除属性,同时验证布尔属性只取决于是否存在。

html
<!doctype html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <title>属性方法示例</title>
</head>
<body>
  <article id="card" data-state="draft"></article>
  <button id="submit" disabled="false">提交</button>
  <pre id="output"></pre>
  <script>
    const card = document.querySelector("#card");
    const button = document.querySelector("#submit");
    const output = document.querySelector("#output");
    const lines = [];

    lines.push(`initial state: ${card.getAttribute("data-state")}`);
    lines.push(`has data-state: ${card.hasAttribute("data-state")}`);

    // setAttribute 会新增或覆盖属性,并把传入值转成字符串
    card.setAttribute("data-state", "published");
    card.setAttribute("aria-label", "已发布课程");
    lines.push(`updated state: ${card.getAttribute("data-state")}`);

    card.removeAttribute("aria-label");
    lines.push(`has aria-label after remove: ${card.hasAttribute("aria-label")}`);
    lines.push(`missing title: ${card.getAttribute("title")}`);

    // 布尔属性只要存在就生效,字符串 "false" 也不例外
    lines.push(`disabled attribute value: ${button.getAttribute("disabled")}`);
    lines.push(`button.disabled: ${button.disabled}`);

    button.removeAttribute("disabled");
    lines.push(`button.disabled after remove: ${button.disabled}`);

    output.textContent = lines.join("\n");
  </script>
</body>
</html>

预期输出

text
initial state: draft
has data-state: true
updated state: published
has aria-label after remove: false
missing title: null
disabled attribute value: false
button.disabled: true
button.disabled after remove: false

最后两行说明,移除 disabled 属性后按钮才真正恢复可用。把属性值写成 "false"、"0" 或空字符串,都不会关闭布尔属性。

布尔属性的正确写法

启用布尔状态时,可以写 setAttribute("disabled", ""),也可以直接设置 button.disabled = true。取消状态时,应调用 removeAttribute("disabled"),或设置 button.disabled = false。

不要使用 setAttribute("disabled", false) 取消禁用。传入的 false 会被转换成字符串 "false",属性仍然存在,按钮仍然禁用。

缺失值与字符串转换

setAttribute 会把数字、布尔值和对象转换成字符串。写入计数值后,getAttribute 读到的仍是字符串;若业务需要数字,应在读取后显式转换。

同样不要使用 setAttribute("title", null) 表示删除。它会写入字符串 "null"。需要让属性真正不存在时,必须调用 removeAttribute。

何时使用属性方法

处理 data-*、aria-*、下载属性或不确定是否存在的属性时,属性方法语义清晰。处理输入框当前值、复选框当前选中状态、按钮当前禁用状态时,DOM 属性通常更符合业务意图。

验证修改是否生效时,可以同时检查 hasAttribute、getAttribute 和相关 DOM 属性,从而区分“标签属性已改变”与“运行时状态已改变”。

按自己的节奏,确认真正理解。

目录与阅读设置

阅读设置

17

记录一个想法

使用 getAttribute 与 setAttribute

0 / 10000 · 笔记保存到账号。未提交的修改会在关闭、离开或刷新后丢失。

登录哪都通