jQuery 鼠标事件之mouseleave() 事件

定义和用法

当鼠标指针离开元素时,会发生 mouseleave 事件。

该事件大多数时候会与 mouseenter 事件一起使用。

mouseleave() 方法触发 mouseleave 事件,或规定当发生 mouseleave 事件时运行的函数。

注释:与 mouseout 事件不同,只有在鼠标指针离开被选元素时,才会触发 mouseleave 事件。如果鼠标指针离开任何子元素,同样会触发 mouseout 事件。

下面我们来看一段实例

<html>
<head>
<meta charset="utf-8">
<script src="http://libs.baidu.com/jquery/1.9.1/jquery.js"></script>
<script type="text/javascript">
x=0;
y=0;
$(document).ready(function(){
  $("div.out").mouseout(function(){
    $(".out span").text(x+=1);
  });
  $("div.leave").mouseleave(function(){
    $(".leave span").text(y+=1);
  });
});
</script>
</head>
<body>
<p>不论鼠标指针离开被选元素还是任何子元素,都会触发 mouseout 事件。</p>
<p>只有在鼠标指针离开被选元素时,才会触发 mouseleave 事件。</p>
<div class="out" style="background-color:lightgray;padding:20px;width:300px;">
<h2 style="background-color:white;">被触发的 Mouseout 事件:<span></span></h2>
</div>
</br>
<div class="leave" style="background-color:lightgray;padding:20px;width:300px;">
<h2 style="background-color:white;">被触发的 Mouseleave 事件:<span></span></h2>
</div>
</body>
</html>
继续学习
||
<html> <head> <meta charset="utf-8"> <script src="http://libs.baidu.com/jquery/1.9.1/jquery.js"></script> <script type="text/javascript"> x=0; y=0; $(document).ready(function(){ $("div.out").mouseout(function(){ $(".out span").text(x+=1); }); $("div.leave").mouseleave(function(){ $(".leave span").text(y+=1); }); }); </script> </head> <body> <p>不论鼠标指针离开被选元素还是任何子元素,都会触发 mouseout 事件。</p> <p>只有在鼠标指针离开被选元素时,才会触发 mouseleave 事件。</p> <div class="out" style="background-color:lightgray;padding:20px;width:300px;"> <h2 style="background-color:white;">被触发的 Mouseout 事件:<span></span></h2> </div> </br> <div class="leave" style="background-color:lightgray;padding:20px;width:300px;"> <h2 style="background-color:white;">被触发的 Mouseleave 事件:<span></span></h2> </div> </body> </html>
提交重置代码
章节
笔记
提问
课件
反馈
捐赠

JQuery 基础入门教程

  • 推荐课程
  • 评论
  • 问答
  • 笔记
  • 课件下载

一辆想出轨的无轨电车

终于看完了鼠标事件,也都尝试了,挺好玩的,也挺常用的

8年前    添加回复 0

Alway.以为

乍一看跟上一节的内容很相似

8年前    添加回复 0

学习ing

当鼠标指针离开元素时,会发生 mouseleave 事件。

8年前    添加回复 0

橱窗的光

当鼠标指针离开元素时,会发生 mouseleave 事件

8年前    添加回复 0

末日的春天

相当于在封装的时候对onmouseover进行阻止冒泡

8年前    添加回复 0

课件暂不提供下载,工作人员正在整理中,后期请多关注该课程~