Attr:value 属性

Baseline 已广泛支持

此特性已相当成熟,可在许多设备和浏览器版本上使用。自 ⁨2015 年 7 月⁩以来,各浏览器均已提供此特性。

value 属性是 Attr 接口的一部分,它包含了属性的值。

一个表示属性值的字符串。

示例

以下示例展示了属性 test 的当前值。点击按钮会将其更改为不同的值,并再次读取它以更新显示的值。

HTML

html
<label test="initial value"></label>

<button>Click me to set test to <code>"a new value"</code>…</button>

<p>
  Current value of the <code>test</code> attribute:
  <output id="result">None.</output>
</p>

JavaScript

js
const element = document.querySelector("label");
const button = document.querySelector("button");
const result = document.querySelector("#result");

const attribute = element.attributes[0];
result.value = attribute.value;

button.addEventListener("click", () => {
  attribute.value = "a new value";
  result.value = attribute.value;
});

规范

规范
DOM
# dom-attr-value

浏览器兼容性