jQuery中 mouseover、mouseout、mouseenter、mouseleave的区别

不论鼠标指针穿过被选元素或其子元素,都会触发 mouseover 事件,与其相对应的是mouseout
只有在鼠标指针穿过被选元素时,才会触发 mouseenter 事件,与其相对应的是mouseleave

一、以下这个例子能很好的帮助我们理解mouseover和mouseenter的区别:

 1 <html>
 2 <head>
 3 <script type="text/javascript" src="/jquery/jquery.js"></script>
 4 <script type="text/javascript">
 5 x=0;
 6 y=0;
 7 $(document).ready(function(){
 8   $("div.over").mouseover(function(){
 9     $(".over span").text(x+=1);
10   });
11   $("div.enter").mouseenter(function(){
12     $(".enter span").text(y+=1);
13   });
14 });
15 </script>
16 </head>
17 <body>
18 <div class="over" style="background-color:lightgray;padding:20px;width:40%;float:left">
19 <h2 style="background-color:white;">被触发的 Mouseover 事件:<span></span></h2>
20 </div>
21
22 <div class="enter" style="background-color:lightgray;padding:20px;width:40%;float:right">
23 <h2 style="background-color:white;">被触发的 Mouseenter 事件:<span></span></h2>
24 </div>
25 </body>
26 </html>

1、 $("div.over").mouseover,当鼠标进入<div></div>标签时会计数加1,当鼠标进入<h2></h2>标签时依然继续计数。<h2>是<div>的子元素

  不论鼠标指针穿过被选元素或其子元素,都会触发 mouseover 事件。

2、 $("div.over").mouseenter,当鼠标进入<div></div>标签时会计数加1,当鼠标进入<h2></h2>标签时就不会再继续计数了。<h2>是<div>的子元素

  只有在鼠标指针穿过被选元素时,才会触发 mouseenter 事件。

二、以下这个例子能很好帮助我们理解mouseout和mouseleave的区别:

 1 <html>
 2 <head>
 3 <script type="text/javascript" src="/jquery/jquery.js"></script>
 4 <script type="text/javascript">
 5 x=0;
 6 y=0;
 7 $(document).ready(function(){
 8   $("div.out").mouseout(function(){
 9     $(".out span").text(x+=1);
10   });
11   $("div.leave").mouseleave(function(){
12     $(".leave span").text(y+=1);
13   });
14 });
15 </script>
16 </head>
17 <body>
18 <div class="out" style="background-color:lightgray;padding:20px;width:40%;float:left">
19 <h2 style="background-color:white;">被触发的 Mouseout 事件:<span></span></h2>
20 </div>
21 <div class="leave" style="background-color:lightgray;padding:20px;width:40%;float:right">
22 <h2 style="background-color:white;">被触发的 Mouseleave 事件:<span></span></h2>
23 </div>
24 </body>
25 </html>

1、 $("div.over").mouseout,当鼠标离开<div></div>标签时会计数加1,当鼠标离开<h2></h2>标签时依然继续计数。<h2>是<div>的子元素

  不论鼠标指针离开被选元素还是任何子元素,都会触发 mouseout 事件。

  如下图所示,在三个蓝点位置会触发mouseout事件。

2、 $("div.over").mouseleave,当鼠标离开<div></div>标签时会计数加1,当鼠标离开<h2></h2>标签时就不会再继续计数了。<h2>是<div>的子元素

  只有在鼠标指针离开被选元素时,才会触发 mouseleave 事件。

  如下图所示,在那个蓝点位置会触发mouseleave事件。

时间: 2024-08-22 13:47:54

jQuery中 mouseover、mouseout、mouseenter、mouseleave的区别的相关文章

mouseover ,mouseout ,mouseenter,mouseleave的区别

昨天写了个mouseover.mouseout 事件,发现频频闪动,研究半天,最后得出结论 mouseover/mouseout:鼠标经过该元素触发,经过其子元素再次触发 mouseenter/mouseleave:只触发一次,不论该元素有多少子元素 所以,使用该类型事件时,换成mouseenter/mouseleave

理解mouseover,mouseout,mouseenter,mouseleave

mouseover定义和用法 当鼠标指针位于元素上方时,会发生 mouseover 事件. 该事件大多数时候会与 mouseout 事件一起使用. mouseover() 方法触发 mouseover 事件,或规定当发生 mouseover 事件时运行的函数. 注释:与 mouseenter 事件不同,不论鼠标指针穿过被选元素或其子元素,都会触发 mouseover 事件. ? 1 2 3 <div class="parent">父亲             <div

mouseover与mouseenter与mousemove的区别mouseout与mouseleave的区别

<html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <script src="script/jquery-1.11.0.min.js" type="text/javascript"></script> <script type="text/javascript">

jQuery中的bind() live() delegate()之间区别分析

jQuery中的bind() live() delegate()之间区别分析 首先,你得要了解我们的事件冒泡(事件传播)的概念,我先看一张图 1.bind方式 $('a').bind('click',function (){ alert('click'); }) 解析:这种方式最简单,jq扫描文档找出所有的a,让将函数绑定到每个元素的click事件上 2.live方式 $('a').live('click',function (){ alert('click'); }) 解析:jq将函数绑定到$

jquery中html 与 text方法的区别

jquery中html 与 text方法的区别 24 May 2012/in 网站设计和开发 /by Bruce 接鉵jquery的时间并不长,以前都是用直接用js写的,现在发现在jquery这个框架用起来很方便,不但代码量少了,使用也比较简单,对于浏览器的兼容问题也不用担心,在使用的过程中也会遇到一些疑问,在html标签中附加子标签时所用的方法html()与text()的区别. 通常在用jquery写ajax时,都会用到html()这个方法,而不用text()这个方法,他们之间有什么区别呢?

jQuery中的closest()和parents()的区别

jQuery中的closest()和parents()的区别 jQuery中closest()和parents()的作用非常相似,都是向上寻找符合选择器条件的元素,但是他们之间有一些细微的差别,官网也给出了说明: .closest() .parents() Begins with the current element Begins with the parent element Travels up the DOM tree until it finds a match for the sup

jQuery中 .bind() .live(). delegate() . on() 的区别

jQuery中   .bind()    .live().   delegate() .   on()  的区别 这几种方法都是绑定事件用到的,但是他们之间有些差别 bind(type,[data],fn) 为每个匹配元素的特定事件绑定事件处理函数 例如: <ul> <a href="#"><li>1111111</li></a> <a href="#"><li>22222</

jQuery 中的children()和 find() 的区别

<!DOCTYPE html> <html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> </head> <body> <ul class="level-1"> <li class="item-i">I</li>

jquery 中 on 和普通绑定的区别

都说on是用于动态添加的元素的事件.但是你不觉得奇怪嘛,难道动态添加的元素就不能用普通的事件绑定,这样说不通啊! 其实是on可以在元素添加到dom之前绑定,而对于普通绑定,只要元素已经出现在了dom中,不论是否动态添加  事件当然也是生效的 $('body').on('click', '.d1', function(event) { //event.preventDefault(); console.log('on0');//ok }); console.log($('.d1')); $('.d