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

ShadowRoot: getHTML() method

· 52CE 我爱测

The **`getHTML()`** method of the ShadowRoot interface is used to serialize a shadow root's DOM to an HTML string.

DOM

The getHTML() method of the ShadowRoot interface is used to serialize a shadow root's DOM to an HTML string.

The method provides an options argument that enables the serialization of child nodes that are shadow roots. The options can be used to include nested shadow roots that have been set as ShadowRoot/serializable, and/or a specified array of ShadowRoot objects, which may be either open or closed.

Without arguments, child nodes that are shadow roots are not serialized, and this method behaves in the same way as reading the value of Element.innerHTML.

Note that some browsers serialize the < and > characters as &lt; and &gt; when they appear in attribute values (see Browser compatibility). This is to prevent a potential security vulnerability (mutation XSS) in which an attacker can craft input that bypasses a sanitization function, enabling a cross-site scripting (XSS) attack.

Syntax

getHTML(options)

Parameters

  • options
    • : An options object with the following optional parameters:
      • serializableShadowRoots
        • : A boolean value that specifies whether to include serializable shadow roots. The default value is false.
      • shadowRoots
        • : An array of ShadowRoot objects to serialize. These are included regardless of whether they are marked as serializable, or if they are open or closed. The default value is an empty array.

Return value

A string that represents the HTML serialization of the shadow root.

Exceptions

None.

Specifications

Browser compatibility

See Also

  • Element.getHTML()
  • Element.innerHTML
  • ShadowRoot.setHTMLUnsafe()
  • Element.setHTMLUnsafe()

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

资料来源与适用范围

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

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

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