这是 实验性技术
检查 浏览器兼容性表格 要小心谨慎在生产中使用这之前。

DOM beforeinput event fires when the value of an <input> , <select> ,或 <textarea> element is about to be modified. The event also applies to elements with contenteditable enabled, and to any element when designMode is turned on.

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.

冒泡 Yes
可取消 Yes
接口 InputEvent
事件处理程序特性 None
Sync / Async Sync
Composed Yes
默认动作 Update the DOM element

范例

This example logs current value of the element immediately before replacing that value with the new one applied to the <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;
}
					

结果

规范

规范 状态
UI Events
The definition of 'beforeinput event' in that specification.
工作草案

浏览器兼容性

The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
beforeinput event Chrome Yes Edge 79 Firefox No IE No Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android No Opera Android Yes Safari iOS Yes Samsung Internet Android Yes

图例

完整支持

完整支持

不支持

不支持

实验。期望将来行为有所改变。

实验。期望将来行为有所改变。

另请参阅

元数据

  • 最后修改: