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

TextFormat

· 52CE 我爱测

The **`TextFormat`** interface represents specific formatting that should be applied to a range of text in an editable text region that's attached to an EditContext instance. The text formatting is requested by the Input Method Editor (IME) window that the user is composing text

EditContext API

The TextFormat interface represents specific formatting that should be applied to a range of text in an editable text region that's attached to an EditContext instance. The text formatting is requested by the Input Method Editor (IME) window that the user is composing text with.

When using one of the default editable regions of the web, such as a <textarea> element, IME composition is handled by the browser and operating system for you. For example, when using Japanese IME in a textarea, on Windows, the following text formats can be applied:

  • When text is being composed with the keyboard, the typed characters have a thin wavy underline:

    A textarea in Microsoft Edge on Windows with some Japanese text being composed from the IME window

  • When the user selects a suggestion from the list of candidates in the IME window, the text is replaced and is underlined with a thick solid line:

    A textarea in Microsoft Edge on Windows with some Japanese text being selected from the IME window

When creating your own custom editable region by using the EditContext API, you need to handle IME composition yourself. You should listen for the EditContext/textformatupdate_event event, which gives you the list of text formats that the IME window wants to apply to the text being composed. You should then update the formatting of the text displayed in your editable region accordingly.

Constructor

  • TextFormat.TextFormat
    • : Returns a new TextFormat instance.

Instance properties

  • TextFormat.rangeStart
    • : The start position of the text range that needs to be formatted with the given text format.
  • TextFormat.rangeEnd
    • : The end position of the text range that needs to be formatted with the given text format.
  • TextFormat.underlineStyle
    • : The style of the underline that needs to be applied to the text range that is being formatted.
  • TextFormat.underlineThickness
    • : The thickness of the underline that needs to be applied to the text range that is being formatted.

Examples

Using the textformatupdate event

In the following example, the textformatupdate event is used to log the various formats that the IME composition window wants to apply to text ranges in the editable element. Note that the event listener callback in this example is only called when using an IME window to compose text.

<div id="editor"></div>
#editor {
  height: 200px;
  background: #eeeeee;
}
const editorEl = document.getElementById("editor");
const editContext = new EditContext(editorEl);
editorEl.editContext = editContext;

editContext.addEventListener("textformatupdate", (e) => {
  // Get the TextFormat instances.
  const formats = e.getTextFormats();

  // Iterate over the TextFormat instances.
  for (const format of formats) {
    console.log(
      `Applying a ${format.underlineThickness} ${format.underlineStyle} underline between ${format.rangeStart} and ${format.rangeEnd}.`,
    );
  }
});

Specifications

Browser compatibility

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

资料来源与适用范围

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

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

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