blob: 17df6b0336a8afcc6079acd06615a995da77bcbe (
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
|
---
title: 'Element: keyup event'
slug: Web/API/Element/keyup_event
translation_of: Web/API/Element/keyup_event
---
<div>{{APIRef}}</div>
<p><strong><code>keyup</code></strong> 事件在按键被松开时触发。</p>
<table class="properties">
<thead>
</thead>
<tbody>
<tr>
<th>冒泡</th>
<td>是</td>
</tr>
<tr>
<th>可取消</th>
<td>是</td>
</tr>
<tr>
<th>接口</th>
<td>{{domxref("KeyboardEvent")}}</td>
</tr>
<tr>
<th>事件处理函数属性</th>
<td>{{domxref("GlobalEventHandlers.onkeyup", "onkeyup")}}</td>
</tr>
</tbody>
</table>
<p><code><a href="/en-US/docs/Web/API/Element/keydown_event">keydown</a></code> 和 <code>keyup</code> 事件提供指出哪个键被按下的代码,而 <code>keypress</code> 指出哪些字符被输入。例如,小写字母 “a” 在 <code>keydown</code> 和 <code>keyup</code> 时会被报告为 65,但在 <code>keypress</code> 时为 97。所有事件均将大写字母 “A” 报告为 65。</p>
<p>从 Firefox 65 开始,<code>keyup</code> 和 <code><a href="/en-US/docs/Web/API/Element/keydown_event">keydown</a></code> 事件在 IME 编辑时也会被触发,以提升 CJKT 用户的跨浏览器兼容性({{bug(354358)}},另请参阅 <a href="https://www.fxsitecompat.com/en-CA/docs/2018/keydown-and-keyup-events-are-now-fired-during-ime-composition/">keydown and keyup events are now fired during IME composition</a> 获取更多有用的详情)。要忽略 IME 编辑时的所有 <code>keyup</code> 事件,请执行以下操作(229 是一个关于被 IME 加工过的事件的 <code>keyCode</code> 的特殊值 ):</p>
<pre class="brush: js">eventTarget.addEventListener("keyup", event => {
if (event.isComposing || event.keyCode === 229) {
return;
}
// do something
});
</pre>
<h2 id="例子">例子</h2>
<h3 id="addEventListener_keyup_例子">addEventListener keyup 例子</h3>
<p>在这个例子中,每当你在 {{HtmlElement("input")}} 元素里松开一个键,将会打印 {{domxref("KeyboardEvent.code")}} 的值。</p>
<pre class="brush: html"><input placeholder="Click here, then press and release a key." size="40">
<p id="log"></p></pre>
<pre class="brush: js">const input = document.querySelector('input');
const log = document.getElementById('log');
input.addEventListener('keyup', logKey);
function logKey(e) {
log.textContent += ` ${e.code}`;
}</pre>
<p>{{EmbedLiveSample("addEventListener_keyup_example")}}</p>
<h3 id="等效的_onkeyup">等效的 onkeyup</h3>
<pre class="brush: js">input.onkeyup = logKey;</pre>
<h2 id="规范">规范</h2>
<table class="standard-table">
<thead>
<tr>
<th scope="col">Specification</th>
<th scope="col">Status</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{SpecName("UI Events", "#event-type-keyup")}}</td>
<td>{{Spec2("UI Events")}}</td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
<p>{{Compat("api.Element.keyup_event")}}</p>
<h2 id="更多">更多</h2>
<ul>
<li><code><a href="/en-US/docs/Web/API/HTMLElement/input_event">input</a></code></li>
<li><code><a href="/en-US/docs/Web/API/Element/keydown_event">keydown</a></code></li>
<li><code><a href="/en-US/docs/Web/API/Element/keypress_event">keypress</a></code></li>
<li><a href="/en-US/docs/Web/API/Document/keyup_event">Document <code>keyup</code> event</a></li>
</ul>
|