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

Sanitizer: removeElement() method

· 52CE 我爱测

The **`removeElement()`** method of the Sanitizer interface sets the specified element be removed from the output when the sanitizer is used.

HTML Sanitizer API

The removeElement() method of the Sanitizer interface sets the specified element be removed from the output when the sanitizer is used.

The method can be used with either an allow configuration or a remove configuration. If used with a remove configuration, the specified element is added to the removeElements array. If used with an allow configuration, the element is removed from the elements array (if present).

Syntax

removeElement(element)

Parameters

  • element
    • : A string indicating the name of the element to be disallowed, or an object with the following properties:
      • name
        • : A string containing the name of the element.
      • namespace
        • : A string containing the namespace of the element. The default namespace is "http://www.w3.org/1999/xhtml".

Return value

true if the operation changed the configuration to disallow the element, and false if the element was already disallowed.

Note that false might be returned if the internal configuration:

  • defines an elements array and the element is already omitted (it does not need to be removed)
  • instead defines the removeElements array and the specified element is already present (and is hence already filtered)

Examples

How to disallow elements

This example shows how removeElement() is used to specify an element to be "disallowed".

<pre id="log"></pre>
#log {
  height: 420px;
  overflow: scroll;
  padding: 0.5rem;
  border: 1px solid black;
}
const logElement = document.querySelector("#log");
function log(text) {
  logElement.textContent = text;
}

JavaScript

The code first creates a new Sanitizer object that initially allows div and script elements, and that replaces span elements with their child elements.

The code then calls removeElement() to add p, <script> and <span> elements to the removeElements list in the configuration. Note that adding <script> and <span> removes the elements from their original lists.

if ("Sanitizer" in window) {
// Create sanitizer using SanitizerConfig
const sanitizer = new Sanitizer({
  elements: ["div", "script"],
  replaceWithChildrenElements: ["span"],
});

// Disallow the <p> element
sanitizer.removeElement("p");

// Disallow the <script> element
sanitizer.removeElement("script");
// Disallow the <span> element
sanitizer.removeElement("span");

// Log the sanitizer configuration
let sanitizerConfig = sanitizer.get();
log(JSON.stringify(sanitizerConfig, null, 2));
} else {
  log("The HTML Sanitizer API is NOT supported in this browser.");
}

[!NOTE] This configuration is provided for demonstration only. Sanitizer configurations should include either just the allowed elements (elements) or just the disallowed elements (removeElements), but not both. In this case only the <div> element is allowed and all other elements will be removed from the input: so the removed elements have no effect.

Results

The final configuration is logged below.

How to disallow elements

Specifications

Browser compatibility

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

资料来源与适用范围

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

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

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