HTMLOutputElement:labels 属性

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since October 2018.

HTMLOutputElement.labels 只读属性返回一个包含与 <output> 元素关联的 <label> 元素的 NodeList

一个包含与 <output> 元素关联的 <label> 元素的 NodeList

示例

HTML

html
<label id="label1" for="test">标签 1</label>
<output id="test">输出</output>
<label id="label2" for="test">标签 2</label>

JavaScript

js
window.addEventListener("DOMContentLoaded", () => {
  const output = document.getElementById("test");
  for (const label of output.labels) {
    console.log(label.textContent); // “标签 1”和“标签 2“
  }
});

规范

Specification
HTML Standard
# dom-lfe-labels-dev

浏览器兼容性

BCD tables only load in the browser