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 菜单,类似于选择地址时让用户填写省,市,区的 ...
随机推荐
- Java后端高频知识点学习笔记1---Java基础
Java后端高频知识点学习笔记1---Java基础 参考地址:牛_客_网 https://www.nowcoder.com/discuss/819297 1.重载和重写的区别 重载:同一类中多个同名方 ...
- <转>Hadoop入门总结
转自:http://www.cnblogs.com/skyme/archive/2012/06/01/2529855.html 第1章 引言 1.1 编写目的 对关于hadoop的文档及资料进行进一步 ...
- 初识shellcode
以前只是知道shellcode就是一段恶意代码,直到今天学习了shellcode的知识,才发现这东西真是博大精深.同时也学习到了一些新的指令,在这里记录一下. 通常pwn题目就是为了拿到shell,目 ...
- 2020腾讯犀牛鸟网络安全T-Star高校挑战赛writeup
签到 浏览器禁用js,在www目录下有 key 命令执行基础 使用 | 可以执行其他命令,在 ../目录下有 key.php cat 一下读出来 你能爆破吗 一开始随便输了个账号的时候不是这个页面,抓 ...
- Java实现HttpGet和HttpPost请求
maven引入JSON处理jar <dependency> <groupId>com.alibaba</groupId> <artifactId>fas ...
- 面试官:HashSet如何保证元素不重复?
本文已收录<Java常见面试题>系列,Git 开源地址:https://gitee.com/mydb/interview HashSet 实现了 Set 接口,由哈希表(实际是 HashM ...
- LuoguB2106 矩阵转置 题解
Content 给定一个 \(n\times m\) 的矩阵 \(A\),求其转置 \(A^\text T\). 数据范围:\(1\leqslant n,m\leqslant 100\). Solut ...
- Nginx 编译数格式化输出
printf "%s\n" `nginx -V 2>&1` nginx -V 2>&1 | sed 's/ /\n/g'
- ACwing1015. 摘花生
题目: Hello Kitty想摘点花生送给她喜欢的米老鼠. 她来到一片有网格状道路的矩形花生地(如下图),从西北角进去,东南角出来. 地里每个道路的交叉点上都有种着一株花生苗,上面有若干颗花生,经过 ...
- JAVA使用netty建立websocket连接
依赖 <!-- https://mvnrepository.com/artifact/commons-io/commons-io --> <dependency> <gr ...