aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/htmlelement/beforeinput_event/index.html
blob: fa11a7588c4b01e9a2081d9afc43db8c0d54e5ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
---
title: 'HTMLElement: beforeinput event'
slug: Web/API/HTMLElement/beforeinput_event
tags:
  - Event
  - InputEvent
  - beforeinput
  - 事件
  - 参考
  - 实验性
translation_of: Web/API/HTMLElement/beforeinput_event
---
<div>{{APIRef}} {{SeeCompatTable}}</div>

<p>DOM 事件 <strong><code>beforeinput</code></strong> 在{{HTMLElement("input")}}, {{HTMLElement("select")}}{{HTMLElement("textarea")}} 的值即将被修改前触发。这个事件也可以在 {{domxref("HTMLElement.contentEditable", "contenteditable")}} 被设置为 <code>true</code> 的元素和打开 {{domxref("Document.designMode", "designMode")}} 后的任何元素上被触发。</p>

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

<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>{{DOMxRef("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>

<h2 id="示例">示例</h2>

<p>这个例子会在 {{HtmlElement("input")}} 元素的值即将被新的值更新前记录下当前的值。</p>

<h3 id="HTML">HTML</h3>

<pre class="brush: html">&lt;input placeholder="Enter some text" name="name"/&gt;
&lt;p id="values"&gt;&lt;/p&gt;</pre>

<h3 id="JavaScript">JavaScript</h3>

<pre class="brush: js">const input = document.querySelector('input');
const log = document.getElementById('values');

input.addEventListener('beforeinput', updateValue);

function updateValue(e) {
  log.textContent = e.target.value;
}</pre>

<h3 id="结果">结果</h3>

<p><iframe class="live-sample-frame sample-code-frame" frameborder="0" id="frame_Examples" src="https://mdn.mozillademos.org/en-US/docs/Web/API/HTMLElement/beforeinput_event$samples/Examples?revision=1609140"></iframe></p>

<h2 id="规范">规范</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">规范</th>
   <th scope="col">状态</th>
  </tr>
  <tr>
   <td>{{SpecName('UI Events', "#event-type-beforeinput", "beforeinput event")}}</td>
   <td>{{Spec2('UI Events')}}</td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>



<p>{{Compat("api.HTMLElement.beforeinput_event")}}</p>

<h2 id="参见">参见</h2>

<ul>
 <li>相关事件:<code><a href="/en-US/docs/Web/API/HTMLElement/input_event">input</a></code></li>
</ul>