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

HTMLElement: editContext property

· 52CE 我爱测

The **`editContext`** property of the HTMLElement interface gets and sets an element's associated EditContext object.

EditContext API

The editContext property of the HTMLElement interface gets and sets an element's associated EditContext object.

The EditContext API can be used to build rich text editors on the web that support advanced text input experiences, such as Input Method Editor (IME) composition, emoji picker, or any other platform-specific editing-related UI surfaces.

Value

An EditContext object or null.

Possible elements

Setting the editContext property only works on certain types of elements:

If you try to set the editContext property on an element that is not one of the above, a NotSupportedError DOMException is thrown.

Element association

Setting the editContext property of an element to an EditContext instance associates that element with the EditContext instance.

The association is one-to-one:

  • An element can only be associated to one EditContext instance.
  • An EditContext instance can only be associated to one element.

If you try to associate an already associated EditContext instance to a different element, a DOMException is thrown.

If you try to associate an other EditContext instance to an element that's already associated, a DOMException is also thrown.

To check whether an element is associated with an EditContext instance already, use the EditContext.attachedElements() method.

Garbage collection

An EditContext instance will keep its associated element alive if it has other live references, even if the associated element is removed from the DOM.

If you want to make sure the element is garbage collected, clear the editContext property of the element.

Examples

Setting an element's editContext property

This example shows how to set the editContext property of a <canvas> element to a new EditContext instance in order to make the element editable.

<canvas id="editor-canvas"></canvas>
const canvas = document.getElementById("editor-canvas");
const editContext = new EditContext();
canvas.editContext = editContext;

Clearing an element's editContext property

This example shows how to clear the editContext property of an editable <canvas> element in order to safely remove the element from the DOM.

<canvas id="editor-canvas"></canvas>
// Create the EditContext and associate it with the canvas element.
const canvas = document.getElementById("editor-canvas");
const editContext = new EditContext();
canvas.editContext = editContext;

// Later, clear the editContext property, and remove the element.
canvas.editContext = null;
canvas.remove();

Specifications

Browser compatibility

See also

  • The EditContext interface.

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

资料来源与适用范围

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

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

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