aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/element/mouseleave_event/index.html
blob: 69455fb00978e9b44a8435a019eb4dbd8b1ed583 (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
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
---
title: mouseleave
slug: Web/API/Element/mouseleave_event
translation_of: Web/API/Element/mouseleave_event
---
<div>{{APIRef}}</div>

<p class="summary">指点设备(通常是鼠标)的指针移出某个元素时,会触发<code>mouseleave</code>事件。</p>

<p><code>mouseleave</code>  和 {{event('mouseout')}} 是相似的,但是两者的不同在于<code>mouseleave</code> 不会冒泡而<code>mouseout</code> 会冒泡。<br>
 这意味着当指针离开元素及其所有后代时,会触发<code>mouseleave</code>,而当指针离开元素或离开元素的后代(即使指针仍在元素内)时,会触发<code>mouseout</code></p>

<table class="standard" style="border: solid 1px;">
 <tbody>
  <tr>
   <td><img alt="mouseenter.png" class="default internal" src="/@api/deki/files/5910/=mouseleave.png"></td>
   <td><img alt="mouseover.png" class="default internal" src="/@api/deki/files/5911/=mouseout.png"></td>
  </tr>
  <tr>
   <td>当离开它们时,一个<code>mouseleave</code>事件被发送到层次结构的每个元素。当指针从文本移动到这里表示的最外面的div之外的区域时,这里4个事件会发送到层次结构的四个元素。</td>
   <td>一个单一的鼠标事件<code>mouseout</code>被发送到DOM树最深的元素,然后它冒泡层次,直到它被处理程序取消或到达根。</td>
  </tr>
 </tbody>
</table>

<p> </p>

<h2 id="一般信息">一般信息</h2>

<dl>
 <dt>规范</dt>
 <dd><a href="https://www.w3.org/TR/DOM-Level-3-Events/#event-type-mouseleave">DOM L3</a></dd>
 <dt>接口</dt>
 <dd>{{domxref("MouseEvent")}}</dd>
 <dt>是否冒泡</dt>
 <dd></dd>
 <dt>是否可取消</dt>
 <dd></dd>
 <dt>对象</dt>
 <dd>Element</dd>
 <dt>默认动作</dt>
 <dd></dd>
</dl>

<h2 id="属性">属性</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>事件目标(DOM树中最顶端的目标)。</td>
  </tr>
  <tr>
   <td><code>type</code> {{readonlyInline}}</td>
   <td>{{domxref("DOMString")}}</td>
   <td>事件的类型。</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")}} (<code>window</code> of the document)</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> 事件: 互补事件的目标(详情查看<a href="/en-US/docs/Web/API/MouseEvent/relatedTarget">relatedTarget</a>)。</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,因为没有按钮按下触发这个事件(鼠标移动触发的事件)。</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>生成事件时施加到触摸或tabdevice的压力量;此值介于0.0(最小压力)和1.0(最大压力)之间。</td>
  </tr>
  <tr>
   <td><code>ctrlKey</code> {{readonlyInline}}</td>
   <td>boolean</td>
   <td>
    <p>当事件触发时,Ctrl键是被按下的,则为<code>true</code> ,否则为<code>false</code></p>
   </td>
  </tr>
  <tr>
   <td><code>shiftKey</code> {{readonlyInline}}</td>
   <td>boolean</td>
   <td>当事件触发时,shift键是被按下的,则为<code>true</code> ,否则为<code>false</code></td>
  </tr>
  <tr>
   <td><code>altKey</code> {{readonlyInline}}</td>
   <td>boolean</td>
   <td>当事件触发时,alt键是被按下的,则为<code>true</code> ,否则为<code>false</code></td>
  </tr>
  <tr>
   <td><code>metaKey</code> {{readonlyInline}}</td>
   <td>boolean</td>
   <td>当事件触发时,meta键是被按下的,则为<code>true</code> ,否则为<code>false</code></td>
  </tr>
 </tbody>
</table>

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

<p><a href="https://developer.mozilla.org/en-US/docs/Web/Events/mouseout#Example"><code>mouseout</code> </a>文档有一个例子,说明了<code>mouseout</code><code>mouseleave</code>之间的区别。</p>

<p>以下示例说明了如何使用<code>mouseout</code>来模拟<code>mouseleave</code>事件的事件委托原则。</p>

<p class="brush: html"> </p>

<pre class="brush: html">&lt;ul id="test"&gt;
  &lt;li&gt;
    &lt;ul class="leave-sensitive"&gt;
      &lt;li&gt;item 1-1&lt;/li&gt;
      &lt;li&gt;item 1-2&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;ul class="leave-sensitive"&gt;
      &lt;li&gt;item 2-1&lt;/li&gt;
      &lt;li&gt;item 2-2&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;script&gt;
  var delegationSelector = ".leave-sensitive";

  document.getElementById("test").addEventListener("mouseout", function( event ) {
    var target = event.target,
        related = event.relatedTarget,
        match;

    // search for a parent node matching the delegation selector
    while ( target &amp;&amp; target != document &amp;&amp; !( 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 &amp;&amp; related != target &amp;&amp; related != document ) {
        related = related.parentNode;
    }

    // exit if this is the case
    if ( related == target ) { return; }

    // the "delegated mouseleave" 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 ) {
    if (typeof elem.matchesSelector === "function") {
      // the matchesSelector is prefixed in most (if not all) browsers
      return elem.matchesSelector( selector );
    } else if (typeof elem.matches === "function") {
      return elem.matches( selector );
​​​​​​​    }
  };
&lt;/script&gt;</pre>

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

{{Compat("api.Element.mouseleave_event")}}

<h2 id="相关链接">相关链接</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>