onmouseleave-ventetilfældet

Definition og brug

Når musepegeren forlader elementet, opstår onmouseleave-ventetilfældet.

Tip:This event is usually used with onmouseenter begivenhedUsed together, this event occurs when the mouse pointer moves over the element.

Tip: The onmouseleave event is similar to onmouseout begivenhed. The only difference is that the onmouseleave event does not bubble (does not propagate up the document hierarchy). See more examples at the bottom of the page.

Example

Example 1

Execute JavaScript when the mouse pointer moves out of the image:

<img onmouseleave="normalImg(this)" src="smiley.gif" alt="Smiley">

Try it yourself

Example 2

This example demonstrates the difference between onmousemove, onmouseleave, and onmouseout events:

<div onmousemove="myMoveFunction()">
  <p id="demo">I will demonstrate onmousemove!</p>
</div>
<div onmouseleave="myLeaveFunction()">
  <p id="demo2">I will demonstrate onmouseleave!</p>
</div>
<div onmouseout="myOutFunction()">
  <p id="demo3">I will demonstrate onmouseout!</p>
</div>

Try it yourself

Syntax

In HTML:

<element onmouseleave="myScript">

Try it yourself

In JavaScript:

object.onmouseleave = function(){myScript};

Try it yourself

In JavaScript, use the addEventListener() method:

object.addEventListener("mouseleave", myScript);

Try it yourself

Note:Internet Explorer 8 or earlier versions do not support addEventListener() method.

Technical details

Bubbling: Not supported
Cancelable: Not supported
Event types: MouseEvent
Supported HTML tags: All HTML elements except: <base>, <bdo>, <br>, <head>, <html>, <iframe>, <meta>, <param>, <script>, <style> and <title>
DOM version: Level 2 Events

Browser supports

Tallene i tabellen angiver den første browserversion, der fuldt ud understøtter begivenheden.

Begivenheder Chrome IE Firefox Safari Opera
onmouseleave 30.0 5.5 Support 6.1 11.5