onmouseleave 事件
在鼠标指针移出指针时执行 JavaScript:
<img onmouseenter="bigImg(this)" onmouseleave="normalImg(this)" border="0" src="/examples/smiley.gif" alt="Smiley" width="32" height="32">
<p>
bigImg() 函数在用户将鼠标指针移动到该图像时触发。</p>
<p>
normalImg() 函数在用户将鼠标指针移出图像时触发。</p>
<script>
function bigImg(x) {
x.style.height = "64px";
x.style.width = "64px";
}
function normalImg(x) {
x.style.height = "32px";
x.style.width = "32px";
}
</script>
在以下更多实例中点 "尝试一下" 查看更多演示。
定义和用法
onmouseleave 事件在鼠标移除元素时触发。
提示: 该事件通常与 onmouseenter 事件一起使用, 该事件在鼠标移动到元素上时触发。
提示: onmouseleave 事件类似于 onmouseout 事件。 唯一的区别是 onmouseleave 事件不支持冒泡 。
浏览器支持
表格中的数字表示支持该事件的第一个浏览器的版本号。
| 事件 | |||||
|---|---|---|---|---|---|
| onmouseleave | 30.0 | 5.5 | Yes | 6.1 | 11.5 |
语法
HTML 中:
<element onmouseleave="myScript">
<p>该实例演示了如何向 h1 元素添加 "onmouseenter" 和 "onmouseleave" 事件。</p>
<h1 id="demo" onmouseenter="mouseEnter()" onmouseleave="mouseLeave()">鼠标移到我这</h1>
<script>
function mouseEnter() {
document.getElementById("demo").style.color = "red";
}
function mouseLeave() {
document.getElementById("demo").style.color = "black";
}
</script>
JavaScript 中:
object.onmouseleave = function() {
myScript
};
<p>改实例使用了 HTML DOM 向 h1 元素添加 "onmouseenter" 和 "onmouseleave" 事件。</p>
<h1 id="demo">Mouse over me</h1>
<script>
document.getElementById("demo").onmouseenter = function() {
mouseEnter()
};
document.getElementById("demo").onmouseleave = function() {
mouseLeave()
};
function mouseEnter() {
document.getElementById("demo").style.color = "red";
}
function mouseLeave() {
document.getElementById("demo").style.color = "black";
}
</script>
JavaScript 中, 使用 addEventListener() 方法:
object.addEventListener("mouseleave", myScript);
<p>该实例使用了 addEventListener() 方法向 h1 元素添加 "mouseenter" 和 "mouseleave" 事件。</p>
<h1 id="demo">Mouse over me</h1>
<script>
document.getElementById("demo").addEventListener("mouseenter", mouseEnter);
document.getElementById("demo").addEventListener("mouseleave", mouseLeave);
function mouseEnter() {
document.getElementById("demo").style.color = "red";
}
function mouseLeave() {
document.getElementById("demo").style.color = "black";
}
</script>
注意: Internet Explorer 8 及更早 IE 版本不支持 addEventListener() 方法。
技术细节
| 是否支持冒泡: | No |
|---|---|
| 是否可以取消: | No |
| 事件类型: | MouseEvent |
| 支持的 HTML 标签: | 所有 HTML 元素,除了:<base>, <bdo>, <br>, <head>, <html>, <iframe>, <meta>, <param>, <script>, <style>, 和 <title> |
更多实例
该实例演示了 onmousemove, onmouseleave 和 onmouseout 事件的区别:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>蜜蜂教程(mifengjc.com)</title>
<style>
div {
width: 100px;
height: 100px;
border: 1px solid black;
margin: 10px;
float: left;
padding: 30px;
text-align: center;
background-color: lightgray;
}
p {
background-color: white;
}
</style>
</head>
<body>
<h3>以下实例演示了 onmousemove, onmouseleave 和 onmouseout 的区别。</h3>
<p> onmousemove 事件在鼠标移动到 div 元素上时触发。</p>
<p> mouseleave 事件只在鼠标指针移出 div 元素时触发。 </p>
<p> onmouseout 事件在鼠标指针移出 div 元素及离开子元素(p 和 span)时触发。</p>
<div onmousemove="myMoveFunction()">
<p>onmousemove: <br> <span id="demo">鼠标移入和移出!</span></p>
</div>
<div onmouseleave="myLeaveFunction()">
<p>onmouseleave: <br> <span id="demo2">鼠标移入和移出!</span></p>
</div>
<div onmouseout="myOutFunction()">
<p>onmouseout: <br> <span id="demo3">鼠标移入和移出!</span></p>
</div>
<script>
x = 0;
y = 0;
z = 0;
function myMoveFunction() {
document.getElementById("demo").innerHTML = z += 1;
}
function myLeaveFunction() {
document.getElementById("demo2").innerHTML = x += 1;
}
function myOutFunction() {
document.getElementById("demo3").innerHTML = y += 1;
}
</script>
</body>
</html>
事件对象