jQuery里的mouseover与mouseenter事件类型区别
JQ里面有mouseover和mouseenter 2个事件类型干着差不多的活,用不好经常出现些小问题。
今天我解释一下原理:
事件类型翻译: mouseover 鼠标移上 mouseenter 鼠标移进
jQuery的 mouseover 绝对等于原生js的 onmouseover,但使用起来是往往会有些问题。
原生js没有 onmouseenter 的事件类型,jQuery内部给它封装了新的事件类型也就是 mouseenter 来解决这个问题。
( 对应还有鼠标离开 onmouseout和mouseleave同理 )
线上demo: http://jsbin.com/gihaya/1/

<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
<meta charset="utf-8">
<title>mouseover和mouseenter区别</title>
<style>
.over-and-enter div{background:#ccc;padding:20px;width:30%;float:left;margin: 0 10px;}
.over-and-enter h2{background: #fff;}
</style>
<script type="text/javascript">
var x=0;
var y=0;
$(function(){
$('.over').mouseover(function(){
$('.over span').html( x+=1 );
});
$('.enter').mouseenter(function(){
$('.enter span').html( y+=1 );
});
});
</script>
</head>
<body>
<div class="over-and-enter">
<p>不论鼠标<b>移上被选元素或其子元素</b>,都会触发 mouseover 事件。</p>
<p>鼠标移进被选元素,不管在其内部如何移动,<b>只触发一次</b> mouseenter 事件。</p>
<div class="over">
<h2>被触发的 mouseover 事件:<span></span></h2>
</div>
<div class="enter">
<h2>被触发的 mouseenter 事件:<span></span></h2>
</div>
</div>
</body>
</html>
工作中可能遇到的bug。
线上demo: http://jsbin.com/lacepe/1/

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>mouseenter和mouseleave</title>
<style>
.father{width:100%;height:200px;background: #ccc;border: 1px solid #000;display: none;cursor: pointer;}
.children{width: 95%;height:150px;background: #eee;margin: 10px auto;cursor: pointer;}
</style>
<script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
<script>
$(function(){
// js原生事件有onmouseover和onmouseout,功能是绝对等于jQ的mouseover和mouseout
$('button').mouseover(function(){
$('.father').show();
});
// jQuery封装了2个新的事件,mouseenter和mouseleave来检测移进/出被选元素的次数
$('.father').mouseout(function(){
$('.father').hide();
});
// demo的bug把mouseout事件类型换成mouseleave即可。 // 总结:
// 我们jquery 里面 不太用 mouseover mouseout
// 而我们喜欢用 mouseenter mouseleave
})
</script>
</head>
<body>
<button>鼠标过来</button>
<div class="father">
我是下拉菜单,<b>离开我的范围我才隐藏起来</b>。
<div class="children">我可能是下拉菜单的内容,<b>移到我这里看看。</b></div>
</div>
</body>
</html>
解决办法:demo的bug把mouseout事件类型换成mouseleave即可。
总结:
我们jquery 里面 不太用 mouseover mouseout
而我们喜欢用 mouseenter mouseleave
jQuery里的mouseover与mouseenter事件类型区别的更多相关文章
- jquery的hover mouseover mouseout mouseenter mouseleave的区别
jquery的hover mouseover mouseout mouseenter mouseleave的区别 1.mouseover mouseout mouseover - 鼠标指针经过任何子元 ...
- jquery的bind跟on绑定事件的区别
jquery的bind跟on绑定事件的区别:主要是事件冒泡: jquery文档中bind和on函数绑定事件的用法: .bind(events [,eventData], handler) .on(ev ...
- 一个例子说明mouseover事件与mouseenter事件的区别
<html> <head> <meta charset="UTF-8"> <title>haha</title> < ...
- mouseover事件与mouseenter事件的区别
不论鼠标指针穿过被选元素或其子元素,都会触发 mouseover 事件.对应mouseout 只有在鼠标指针穿过被选元素时,才会触发 mouseenter 事件.对应mouseleave 被触发的 M ...
- 关于事件mouseover ,mouseout ,mouseenter,mouseleave的区别
轮播中大多会选择mouseover和mouseout 这个时候是没有任何问题的 但当遇到有css3动画的时候,会发现移入移出过快 动画还没加载完成就需要执行下一个动画,完了动画样式就错乱了. 这时候 ...
- mouseover,mouseout,mouseenter,mouseleave的区别
1.前言 今天下午参加一个面试,对方问我写不写博客,这时候才猛然意识到好久没写东西了.最近一直在外边实习,每天有很多经历和挑战,但是却没有及时地记录下来,这一点必须得批评自己,以后得经常把自己遇到的问 ...
- setTimeout用于取消多次执行mouseover或者mouseenter事件,间接实现hover的悬停加载的效果.
Mouseenter在鼠标滑上去不会对其子元素也发生监听, Mouseover在鼠标滑上去会对其子元素发生监听. 所以对于事件的监听,我们要看需求,这里是对父元素的监听,不需要对子元素做监听.就用mo ...
- JQuery中的mouseover和mouseenter的区别
mouseover和mouseout是一对:mouseenter和mouseleave是一对. 相同点:都是鼠标经过就触发事件 不同点: 给外盒子一个经过触发事件,但是mouseover会在鼠标经过外 ...
- EasyUI 中 Combobox里的onChange和onSelect事件的区别
EasyUI 中 Combobox 选项发生改变时会触发 onChange,onSelect,onClick 3 个事件. 最近要做一个级联的 Combo 菜单,类似于选择地址时让用户填写省,市,区的 ...
随机推荐
- 安装xampp开发环境更改默认项目路径
xampp开发环境中默认的项目路径在xampp下的htdocs文件下 如果想修改默认项目的位置步骤如下: 1)D:\xampp\apache\conf 找到httpd.conf打开 2)找到 Docu ...
- NTLM验证过程
中我们介绍Kerberos认证的整个流程.在允许的环境下,Kerberos是首选的认证方式.在这之前,Windows主要采用另一种认证协议 --NTLM(NT Lan Manager).NTLM使用在 ...
- CF125A Measuring Lengths in Baden 题解
Content 在 Baden,一英寸等于 \(3\) 厘米,一英尺等于 \(12\) 英寸. 现在有一个 \(n\) 厘米的物体,求在 Baden,它是几英尺又几英寸. 数据范围:\(1\leqsl ...
- CF157A Game Outcome 题解
Content 有一个 \(n\times n\) 的矩阵,每个元素都有一个权值.求所有满足其所在纵列所有元素权值和大于其所在横列所有元素权值和的元素个数. 数据范围:\(1\leqslant n\l ...
- CF1501A Alexey and Train 题解
Content 一列火车从 \(0\) 时刻开始从 \(1\) 号站出发,要经过 \(n\) 个站,第 \(i\) 个站的期望到达时间和离开时间分别为 \(a_i\) 和 \(b_i\),并且还有一个 ...
- 七种可能 | Linux丢包故障的定位与解决
出处[云英公众号]:https://mp.weixin.qq.com/s?__biz=MzI1NzM3NTYxMw==&mid=2247483685&idx=1&sn=95c8 ...
- docker查看容器元数据、详细信息,查看容器挂载的目录
通过 docker inspect 175f 查看容器元数据 我们启动docker的时候会挂载目录,但是挂载之后 后面就忘了 如何查看挂载的目录位置呢 可以通过 docker inspect a7a6 ...
- JAVA匹配html中所有img标签
public static List<String> getImg(String htmlStr) { List<String> list = new ArrayList< ...
- JAVA比较两个版本号的大小
/** * 比较版本号的大小 (两个版本号格式应尽量相同) * * @param v1 版本号1 * @param v2 版本号2 * @return 正数:v1大 负数:v2大 0:相等 */ pu ...
- c++读取文件操作和写入文件
在C++中与读取文件和写入文件简单操作有关的类分别有ifstream(文件读入).ofstream(文件写出).fstream (文件读入和写出). 名称 作用 ifstream 文件读入 ofstr ...