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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
|
---
title: mouseenter
slug: Web/API/Element/mouseenter_event
tags:
- API
- Event
- mouseenter
- 鼠标移入事件
translation_of: Web/API/Element/mouseenter_event
---
<div>{{APIRef}}</div>
<p>当定点设备(通常指鼠标)移动到元素上时就会触发 <code>mouseenter 事件</code></p>
<p>类似 {{event('mouseover')}},它们两者之间的差别是 <code>mouseenter </code>不会<a href="/zh-CN/docs/Web/API/Event/bubbles">冒泡</a>(bubble),也就是说当指针从它的子层物理空间移到它的物理空间上时不会触发</p>
<div style="-moz-column-width: 455px; -webkit-column-width: 455px; column-width: 455px; border: solid 1px; padding: 5px; margin-bottom: 10px;">
<div style="text-align: center;"><img alt="mouseenter.png" class="default internal" src="/@api/deki/files/5908/=mouseenter.png"></div>
(mouseenter事件)触发时,会向层次结构的每个元素发送一个mouseenter事件。当指针到达文本时,此处将4个事件发送到层次结构的四个元素。
<div style="text-align: center;"><img alt="mouseover.png" class="default internal" src="/@api/deki/files/5909/=mouseover.png"></div>
一个单独的mouseover事件被发送到DOM树的最深层元素,然后它将层次结构向上冒泡,直到它被处理程序取消或到达根目录。</div>
<p>对于深层次结构,发送的mouseenter事件数量可能非常大并且会导致严重的性能问题。在这种情况下,最好是监听鼠标悬停事件。(可使用chrome开发者工具选项卡中的Performance进行性能测试)</p>
<p>结合其对称事件, <code>mouseleave</code>, mouseenter DOM事件的行为方式与CSS {{cssxref(':hover')}} 伪类非常相似。.</p>
<h2 id="General_info">General info</h2>
<dl>
<dt style="float: left; text-align: right; width: 120px;">Specification</dt>
<dd style="margin: 0 0 0 120px;"><a class="external" href="http://www.w3.org/TR/DOM-Level-3-Events/#event-type-mouseenter">DOM L3</a></dd>
<dt style="float: left; text-align: right; width: 120px;">Interface</dt>
<dd style="margin: 0 0 0 120px;">{{domxref('MouseEvent')}}</dd>
<dt style="float: left; text-align: right; width: 120px;">Synchronicity</dt>
<dd style="margin: 0 0 0 120px;">Synchronous</dd>
<dt style="float: left; text-align: right; width: 120px;">Bubbles</dt>
<dd style="margin: 0 0 0 120px;">No</dd>
<dt style="float: left; text-align: right; width: 120px;">Cancelable</dt>
<dd style="margin: 0 0 0 120px;">No</dd>
<dt style="float: left; text-align: right; width: 120px;">Target</dt>
<dd style="margin: 0 0 0 120px;">Element</dd>
<dt style="float: left; text-align: right; width: 120px;">Default Action</dt>
<dd style="margin: 0 0 0 120px;">None</dd>
</dl>
<h2 id="Properties_属性列表">Properties 属性列表</h2>
<table class="standard-table">
<thead>
<tr>
<th scope="col">Property 属性</th>
<th scope="col">Type 类型</th>
<th scope="col">Description 描述</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>target</code> {{readonlyInline}}</td>
<td>{{domxref("EventTarget")}}</td>
<td>事件(event)目标(DOM树中最顶层的目标)。</td>
</tr>
<tr>
<td><code>type</code> {{readonlyInline}}</td>
<td>{{domxref("DOMString")}}</td>
<td>事件(event)类型.</td>
</tr>
<tr>
<td><code>bubbles</code> {{readonlyInline}}</td>
<td>Boolean</td>
<td>这个事件是否冒泡</td>
</tr>
<tr>
<td><code>cancelable</code> {{readonlyInline}}</td>
<td><code>Boolean</code></td>
<td>这个事件可否取消</td>
</tr>
<tr>
<td><code>view</code> {{readonlyInline}}</td>
<td>{{domxref("WindowProxy")}}</td>
<td>{{domxref("document.defaultView")}} (document的 <code>window</code> )</td>
</tr>
<tr>
<td><code>detail</code> {{readonlyInline}}</td>
<td><code>long</code> (<code>float</code>)</td>
<td>0.</td>
</tr>
<tr>
<td><code>currentTarget</code> {{readonlyInline}}</td>
<td>{{domxref("EventTarget")}}</td>
<td>事件监听器监听的节点</td>
</tr>
<tr>
<td><code>relatedTarget</code> {{readonlyInline}}</td>
<td>{{domxref("EventTarget")}}</td>
<td>对于 <code>mouseover</code>, <code>mouseout</code>, <code>mouseenter</code> 和 <code>mouseleave</code> 事件: the target of the complementary event (the <code>mouseleave</code> target in the case of a <code>mouseenter</code> event). <code>null</code> otherwise.</td>
</tr>
<tr>
<td><code>screenX</code> {{readonlyInline}}</td>
<td>long</td>
<td>鼠标指针相对于屏幕的X轴坐标</td>
</tr>
<tr>
<td><code>screenY</code> {{readonlyInline}}</td>
<td>long</td>
<td>鼠标指针相对于屏幕的Y轴坐标</td>
</tr>
<tr>
<td><code>clientX</code> {{readonlyInline}}</td>
<td>long</td>
<td>鼠标指针相对于DOM元素的X轴坐标</td>
</tr>
<tr>
<td><code>clientY</code> {{readonlyInline}}</td>
<td>long</td>
<td>鼠标指针相对于DOM元素的Y轴坐标</td>
</tr>
<tr>
<td><code>button</code> {{readonlyInline}}</td>
<td>unsigned short</td>
<td>始终为0,因为没有按钮会触发这个事件 (mouse movement事件干的).</td>
</tr>
<tr>
<td><code>buttons</code> {{readonlyInline}}</td>
<td>unsigned short</td>
<td>表明当事件触发时鼠标上按下的键: 左键=1, 右键=2, 中键 (鼠标滚轮) =4, 第四个按钮 (通常是 “浏览器返回上一个页面”按钮)=8, 第五个按钮 (通常是“浏览器向前导航”按钮)=16. 如果很多按钮同时按下, 那就返回这些值的逻辑计算值. 比如,当左键和右键同时按下时返回3 (=1 | 2). <a href="/en-US/docs/Web/API/MouseEvent">了解更多</a>.</td>
</tr>
<tr>
<td><code>mozPressure</code> {{readonlyInline}}</td>
<td>float</td>
<td>The amount of pressure applied to a touch or tabdevice when generating the event; this value ranges between 0.0 (minimum pressure) and 1.0 (maximum pressure).</td>
</tr>
<tr>
<td><code>ctrlKey</code> {{readonlyInline}}</td>
<td>boolean</td>
<td><code>true</code> 代表当事件触发时按着ctrl键. <code>false</code> 反之.</td>
</tr>
<tr>
<td><code>shiftKey</code> {{readonlyInline}}</td>
<td>boolean</td>
<td><code>true</code> 代表当事件触发时按着shift键. <code>false</code> 反之.</td>
</tr>
<tr>
<td><code>altKey</code> {{readonlyInline}}</td>
<td>boolean</td>
<td><code>true</code> 代表当事件触发时按着alt键. <code>false</code> 反之.</td>
</tr>
<tr>
<td><code>metaKey</code> {{readonlyInline}}</td>
<td>boolean</td>
<td><code>true</code> 代表当事件触发时按着meta键 (???) <code>false</code> 反之.</td>
</tr>
</tbody>
</table>
<h2 id="Examples">Examples</h2>
<p>鼠标悬停( <a href="/en-US/docs/Web/Events/mouseover#Example"><code>mouseover</code> </a>)文档中有一个示例说明了mouseover和mouseenter之间的区别。</p>
<p>以下示例说明如何使用mouseover模拟mouseenter事件的事件委派原则。</p>
<pre class="brush: html"><ul id="test">
<li>
<ul class="enter-sensitive">
<li>item 1-1</li>
<li>item 1-2</li>
</ul>
</li>
<li>
<ul class="enter-sensitive">
<li>item 2-1</li>
<li>item 2-2</li>
</ul>
</li>
</ul>
<script>
var delegationSelector = ".enter-sensitive";
document.getElementById("test").addEventListener("mouseover", function( event ) {
var target = event.target,
related = event.relatedTarget,
match;
// search for a parent node matching the delegation selector
while ( target && target != document && !( match = matches( target, delegationSelector ) ) ) {
target = target.parentNode;
}
// exit if no matching node has been found
if ( !match ) { return; }
// loop through the parent of the related target to make sure that it's not a child of the target
while ( related && related != target && related != document ) {
related = related.parentNode;
}
// exit if this is the case
if ( related == target ) { return; }
// the "delegated mouseenter" handler can now be executed
// change the color of the text
target.style.color = "orange";
// reset the color after a small amount of time
setTimeout(function() {
target.style.color = "";
}, 500);
}, false);
// function used to check if a DOM element matches a given selector
// the following code can be replaced by this IE8 compatible function: https://gist.github.com/2851541
function matches( elem, selector ){
// the webkitMatchesSelector is prefixed in most (if not all) browsers
return elem.webkitMatchesSelector( selector );
};
</script></pre>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>{{CompatibilityTable}}</p>
<div id="compat-desktop">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Chrome</th>
<th>Edge</th>
<th>Firefox (Gecko)</th>
<th>Internet Explorer</th>
<th>Opera</th>
<th>Safari</th>
</tr>
<tr>
<td>Basic support</td>
<td>30<sup>[1]</sup></td>
<td>{{CompatVersionUnknown}}</td>
<td>10<sup>[2]</sup></td>
<td>5.5</td>
<td>{{CompatVersionUnknown}}<br>
{{CompatNo}} 15.0<br>
17.0</td>
<td>7<sup>[3]</sup></td>
</tr>
<tr>
<td>On disabled form elements</td>
<td>{{CompatNo}}</td>
<td>{{CompatNo}}</td>
<td>{{CompatGeckoDesktop("44.0")}}<sup>[4]</sup></td>
<td>{{CompatNo}}</td>
<td>{{CompatNo}}</td>
<td>{{CompatUnknown}}</td>
</tr>
</tbody>
</table>
</div>
<div id="compat-mobile">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Android</th>
<th>Chrome for Android</th>
<th>Edge</th>
<th>Firefox Mobile (Gecko)</th>
<th>IE Mobile</th>
<th>Opera Mobile</th>
<th>Safari Mobile</th>
</tr>
<tr>
<td>Basic support</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatUnknown}}</td>
</tr>
<tr>
<td>On disabled form elements</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatNo}}</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatUnknown}}</td>
</tr>
</tbody>
</table>
</div>
<p>[1] Implemented in <a href="https://crbug.com/236215">bug 236215</a>.</p>
<p>[2] Implemented in {{bug("432698")}}.</p>
<p>[3] Safari 7 fires the event in many situations where it's not allowed to, making the whole event useless. See <a href="https://crbug.com/470258">bug 470258</a> for the description of the bug (it existed in old Chrome versions as well). Safari 8 has correct behavior</p>
<p>[4] Implemented in {{bug("218093")}}.</p>
<h2 id="See_also">See also</h2>
<ul>
<li>{{Event("mousedown")}}</li>
<li>{{Event("mouseup")}}</li>
<li>{{Event("mousemove")}}</li>
<li>{{Event("click")}}</li>
<li>{{Event("dblclick")}}</li>
<li>{{Event("mouseover")}}</li>
<li>{{Event("mouseout")}}</li>
<li>{{Event("mouseenter")}}</li>
<li>{{Event("mouseleave")}}</li>
<li>{{Event("contextmenu")}}</li>
</ul>
|