值
一个表示属性值的字符串。
示例
以下示例展示了属性 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 |
浏览器兼容性
加载中…