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

Element:input 事件

· 52CE 我爱测

当一个 input、select 或 textarea 元素的 `value` 被修改时,会触发 **`input`** 事件。

当一个 input、select 或 textarea 元素的 value 被修改时,会触发 input 事件。

<table class="properties"> <tbody> <tr> <th scope="row">Bubbles</th> <td>Yes</td> </tr> <tr> <th scope="row">Cancelable</th> <td>No</td> </tr> <tr> <th scope="row">Interface</th> <td>InputEvent</td> </tr> <tr> <th scope="row">Event handler property</th> <td>GlobalEventHandlers.oninput</td> </tr> </tbody> </table>

input 事件也适用于启用了 HTMLElement.contentEditable 的元素,以及开启了 Document.designMode 的任意元素。在contenteditable 和 designMode 的情况下,事件的 target 为当前正在编辑的宿主。如果这些属性应用于多个元素上,当前正在编辑的宿主为最近的父节点不可编辑的祖先元素。

对于 type=checkbox 或 type=radio 的 input 元素,每当用户切换控件(通过触摸、鼠标或键盘)时(HTML5 规范),input 事件都应该触发。然而,历史事实并非如此。请检查兼容性,或使用 change 事件代替这些类型的元素。

[!NOTE] 每当元素的 value 改变,input 事件都会被触发。这与 HTMLInputElement.change_event 事件不同。change 事件仅当 value 被提交时触发,如按回车键,从一个 options 列表中选择一个值等。

示例

每当用户修改 input 元素的 value 时,这个示例会记录 value。

HTML

<input placeholder="输入一些文本" name="name" />
<p id="values"></p>

JavaScript

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

input.addEventListener("input", updateValue);

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

结果

示例

属性

Property Type Description
target EventTarget The event target (the topmost target in the DOM tree).
type String The type of event.
bubbles Boolean Whether the event normally bubbles or not.
cancelable Boolean Whether the event is cancellable or not.

规范

浏览器兼容性

参见

  • 相关事件
    • Element/beforeinput_event
    • HTMLElement/change_event
    • HTMLInputElement/invalid_event

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

资料来源与适用范围

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

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

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