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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
---
title: compositionstart
slug: Web/API/Element/compositionstart_event
tags:
- Element
- Event
- Input method
- compositionstart
- 事件
- 参考
translation_of: Web/API/Element/compositionstart_event
original_slug: Web/Events/compositionstart
---
<div>{{APIRef}}</div>
<p>文本合成系统如 {{glossary("input method editor")}}(即输入法编辑器)开始新的输入合成时会触发 <strong><code>compositionstart</code></strong> 事件。</p>
<p>例如,当用户使用拼音输入法开始输入汉字时,这个事件就会被触发。</p>
<table class="properties">
<tbody>
<tr>
<td><strong>Bubbles</strong></td>
<td>Yes</td>
</tr>
<tr>
<td><strong>Cancelable</strong></td>
<td>Yes</td>
</tr>
<tr>
<td><strong>Interface</strong></td>
<td>{{domxref("CompositionEvent")}}</td>
</tr>
<tr>
<td><strong>Event handler property</strong></td>
<td>
<table>
<tbody>
<tr>
<td>None</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
<h2 id="示例">示例</h2>
<pre>const inputElement = document.querySelector('input[type="text"]');
inputElement.addEventListener('compositionstart', (event) => {
console.log(`generated characters were: ${event.data}`);
});</pre>
<h3 id="Live_example">动态演示</h3>
<h4 id="HTML">HTML</h4>
<pre class="brush: html"><div class="control">
<label for="name">On macOS, click in the textbox below,<br> then type <kbd>option</kbd> + <kbd>`</kbd>, then <kbd>a</kbd>:</label>
<input type="text" id="example" name="example">
</div>
<div class="event-log">
<label>Event log:</label>
<textarea readonly class="event-log-contents" rows="8" cols="25"></textarea>
<button class="clear-log">Clear</button>
</div></pre>
<div class="hidden">
<h4 id="CSS">CSS</h4>
<pre class="brush: css">body {
padding: .2rem;
display: grid;
grid-template-areas: "control log";
}
.control {
grid-area: control;
}
.event-log {
grid-area: log;
}
.event-log-contents {
resize: none;
}
label, button {
display: block;
}
input[type="text"] {
margin: .5rem 0;
}
kbd {
border-radius: 3px;
padding: 1px 2px 0;
border: 1px solid black;
}
</pre>
</div>
<h4 id="JS">JS</h4>
<pre class="brush: js">const inputElement = document.querySelector('input[type="text"]');
const log = document.querySelector('.event-log-contents');
const clearLog = document.querySelector('.clear-log');
clearLog.addEventListener('click', () => {
log.textContent = '';
});
function handleEvent(event) {
log.textContent = log.textContent + `${event.type}: ${event.data}\n`;
}
inputElement.addEventListener('compositionstart', handleEvent);
inputElement.addEventListener('compositionupdate', handleEvent);
inputElement.addEventListener('compositionend', handleEvent);
</pre>
<h4 id="结果">结果</h4>
<p>{{ EmbedLiveSample('Live_example', '100%', '180px') }}</p>
<h2 id="规范">规范</h2>
<table>
<thead>
<tr>
<th scope="col">规范</th>
<th scope="col">状态</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{SpecName('UI Events', '#event-type-compositionstart')}}</td>
<td>{{Spec2('UI Events')}}</td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
<p>{{Compat}}</p>
<h2 id="参见">参见</h2>
<ul>
<li>相关事件:{{domxref("Element/compositionend_event", "compositionend")}}, {{domxref("Element/compositionupdate_event", "compositionupdate")}}.</li>
</ul>
|