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

Element:beforeinput 事件

· 52CE 我爱测

DOM 事件 **`beforeinput`** 在input, select 或 textarea 的值即将被修改前触发。这个事件也可以在 HTMLElement.contentEditable 被设置为 `true` 的元素和打开 Document.designMode 后的任何元素上被触发。

DOM 事件 beforeinput 在input, select 或 textarea 的值即将被修改前触发。这个事件也可以在 HTMLElement.contentEditable 被设置为 true 的元素和打开 Document.designMode 后的任何元素上被触发。

In the case of contenteditable and designMode, the event target is the editing host. If these properties apply to multiple elements, the editing host is the nearest ancestor element whose parent isn't editable.

<table class="properties"> <tbody> <tr> <th>Bubbles</th> <td>Yes</td> </tr> <tr> <th>Cancelable</th> <td>Yes</td> </tr> <tr> <th>Interface</th> <td>InputEvent</td> </tr> <tr> <th>Event handler property</th> <td>None</td> </tr> <tr> <th>Sync / Async</th> <td>Sync</td> </tr> <tr> <th>Composed</th> <td>Yes</td> </tr> <tr> <th>Default Action</th> <td>Update the DOM element</td> </tr> </tbody> </table>

示例

这个例子会在 input 元素的值即将被新的值更新前记录下当前的值。

HTML

<input placeholder="Enter some text" name="name" />
<p id="values"></p>

JavaScript

const input = document.querySelector("input");
const log = document.getElementById("values");

input.addEventListener("beforeinput", updateValue);

function updateValue(e) {
  log.textContent = e.target.value;
}

结果

示例

规范

浏览器兼容性

参见

  • 相关事件:Element/input_event

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

资料来源与适用范围

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

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

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