当 <input>
、<select>
或 <textarea>
元素的值更改时,DOM input
事件会同步触发。对于 type=checkbox
或 type=radio
的 input
元素,当每次切换控件(通过触摸、鼠标或键盘)时(HTML5规范),input
事件都应该触发,但从历史来看,情况并非如此。所以使用前请检查浏览器兼容性,或者对于这几种元素的类型, 使用 change
事件替代。
此外,当 contenteditable
编辑器的内容被更改时,input
事件也会触发。在这种情况下,事件目标是被编辑的主元素。当有两个或多个具有 contenteditable
为真的元素时,“被编辑的主元素”是指最近的其父级不可编辑的祖先元素。同样,它也会在 designMode
编辑器的根元素上触发。
基本信息
- 规范
- HTML5, DOM Level 3 Events
- Interface
Event
,InputEvent
- 冒泡
- Yes
- 可取消
- No
- 目标
- Element
- 默认操作
- 被修改的值或内容。
属性
Property | Type | Description |
---|---|---|
target 只读 |
EventTarget |
The event target (the topmost target in the DOM tree). |
type 只读 |
DOMString |
The type of event. |
bubbles 只读 |
Boolean |
Whether the event normally bubbles or not. |
cancelable 只读 |
Boolean |
Whether the event is cancellable or not. |
浏览器兼容性
We're converting our compatibility data into a machine-readable JSON format.
This compatibility table still uses the old format,
because we haven't yet converted the data it contains.
Find out how you can help!
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | (Yes) | (Yes)[1] | 9[2] | (Yes)[3] | (Yes) |
紧随 compositionupdate 之后触发 |
(Yes) | 12 (12) | (Yes) | 未实现 | (Yes) |
可用于 contenteditable 元素 |
(Yes) | 14 (14) | 未实现[4] | 15 | (Yes) |
在 designMode 为 "on" 时 |
(Yes) | 14 (14) | 未实现 | 未实现 | (Yes) |
data |
? | 未实现 | ? | ? | ? |
isComposing |
未实现 | 31 (31) | 未实现 | 未实现 | 未实现 |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | (Yes) | ? | ? | (Yes) |
紧随 compositionupdate 之后触发 |
? | ? | 12.0 (12) | ? | ? | ? |
可用于 contenteditable 元素 |
? | ? | 14.0 (14) | ? | ? | ? |
在 designMode 为 "on" 时 |
? | ? | 14.0 (14) | ? | ? | ? |
data |
? | ? | 未实现 | ? | ? | ? |
isComposing |
未实现 | 未实现 | 31.0 (31) | 未实现 | 未实现 | 未实现 |
[1] 在 Gecko 12.0 (Firefox 12.0 / Thunderbird 12.0 / SeaMonkey 2.9) 之前,用户在输入法中输入时,或者 dead keys were used on Mac OS X 时,Gecko 不触发 input 事件。
[2] IE 9 在用户删除输入的文字时不触发 input 事件(例如,按 Backspace 或者删除键,或者“剪切”文字).
[3] Opera 在用户把文字拖进输入框时,不触发 input 事件。
[4] 事件 target 是光标所在的最内侧的元素。
参见
此外, change
事件是相关的。change 触发的频率少于 input
- 它只会在用户提交更改时触发。