大网数据广告活动
Web 标准与前端开发

HTMLOutputElement: HTMLOutputElement() constructor

· 52CE 我爱测

The **`HTMLOutputElement()`** constructor creates a new HTMLOutputElement object.

HTML DOM

The HTMLOutputElement() constructor creates a new HTMLOutputElement object.

[!NOTE] Currently only Safari implements this constructor, so using Document.createElement() is recommended for broader compatibility — see the example below.

Syntax

new HTMLOutputElement()

Parameters

None.

Return value

A new HTMLOutputElement object.

Exceptions

  • TypeError
    • : Thrown with the message "Illegal constructor" in browsers that do not support this constructor.

Examples

Creating an output element programmatically

[!NOTE] In practice, you would usually create output elements using Document.createElement() instead of this constructor, since createElement() is supported across all browsers.

This example creates an output element using the HTMLOutputElement() constructor and inserts it into a form that adds two numbers together.

<form id="my-form">
  <label>
    Number one
    <input type="number" id="a" value="5" />
  </label>
  +
  <label>
    Number two
    <input type="number" id="b" value="3" />
  </label>
  =
  <span id="output-container"></span>
</form>
<p id="warning" hidden>
  ⚠️ Your browser does not support the
  <code>HTMLOutputElement()</code> constructor.
</p>
body {
  font-family: system-ui;
}

input {
  width: 3rem;
  font-size: inherit;
}

p {
  padding: 0.25rem;
  background-color: #fff2cc;
}
try {
  new HTMLOutputElement();
} catch {
  document.getElementById("warning").hidden = false;
}

const output = new HTMLOutputElement();
output.id = "result";
output.setAttribute("for", "a b");
document.getElementById("output-container").appendChild(output);

function updateResult() {
  const a = document.getElementById("a");
  const b = document.getElementById("b");
  output.value = a.valueAsNumber + b.valueAsNumber;
}

document.getElementById("my-form").addEventListener("input", updateResult);
updateResult();

creating_an_output_element_programmatically

Specifications

Browser compatibility

See also

  • HTMLOutputElement
  • output
  • Document.createElement()

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。