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

TextEvent

· 52CE 我爱测

The **`TextEvent`** interface is a legacy UI event interface for reporting changes to text UI elements.

UI Events

The TextEvent interface is a legacy UI event interface for reporting changes to text UI elements.

[!NOTE] TextEvent events have been superseded by events such as input, beforeinput, keypress, keyup, and keydown.

Instance properties

This interface also inherits properties from its parent UIEvent, and indirectly from Event.

  • TextEvent.data
    • : Indicates the data associated with the event.

Instance methods

  • TextEvent.initTextEvent()
    • : Populates the values of this (new) TextEvent with the given parameters.

Events list

The following is a list of all TextEvent events:

  • textinput

Examples

Listen for text input events

You can register a listener for text input events using EventTarget.addEventListener() as follows:

element.addEventListener("textInput", (event) => {
  // …
});

Simple logger showing input events

This example listens for a number of events that are fired on an input, including textInput. The event type and the event data are logged, allowing you to see where textInput is emitted relative to the other events such as those generated by key presses.

HTML

<input placeholder="Enter some text" name="name" />
<pre id="log"></pre>
#log {
  height: 140px;
  overflow: scroll;
  padding: 0.5rem;
  border: 1px solid black;
}

JavaScript

const logElement = document.querySelector("#log");
function log(text) {
  logElement.innerText = `${logElement.innerText}${text}\n`;
  logElement.scrollTop = logElement.scrollHeight;
}
const input = document.querySelector("input");

input.addEventListener("keypress", updateValue);
input.addEventListener("keyup", updateValue);
input.addEventListener("keydown", updateValue);
input.addEventListener("input", updateValue);
input.addEventListener("beforeinput", updateValue);
input.addEventListener("textInput", updateValue);

function updateValue(e) {
  log(`${e.type}: ${e.data}`);
}

Result

Simple logger showing input events

Specifications

Browser compatibility

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

资料来源与适用范围

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

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

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