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 菜单,类似于选择地址时让用户填写省,市,区的 ...
随机推荐
- ORA-31633:unable to create master table "DP.SYS_EXPORT_FULL_11" ORA-01658
问题描述:在进行数据泵进行数据库备份的时候,但是导出命令报错,环境是19C 4节点的rac 一体机.目前磁盘空间需要清理,清理之前先备份一下数据库 ORA-31626:job does not exi ...
- 『学了就忘』Linux系统管理 — 84、Linux中进程的管理
目录 1.Linux系统中的信号 2.杀掉进程的命令 (1)kill命令 (2)killall命令 (3)pkill命令 1.Linux系统中的信号 Linux系统中可以识别的信号较多,我们可以使用命 ...
- [BUUCTF]PWN——[BJDCTF 2nd]ydsneedgirlfriend2
[BJDCTF 2nd]ydsneedgirlfriend2 附件 步骤: 例行检查,64位程序,开启了canary和nx 试运行一下程序,看看大概的情况,经典的堆块的布局 64位ida载入,习惯性的 ...
- CF535A Tavas and Nafas 题解
Content 请输出整数 \(s\) 的英文写法. 数据范围:\(0\leqslant s\leqslant 99\). Solution 直接对应打表即可. 当 \(0\leqslant s\le ...
- python enumerate枚举用法总结
enumerate()说明 enumerate()是python的内置函数enumerate在字典上是枚举.列举的意思对于一个可迭代的(iterable)/可遍历的对象(如列表.字符串),enumer ...
- RIP2与OSPFv2 动态路由协议区别
OSPF五种报文解析 Hello:招呼信息 Route-ID:换回口地址/活动的物理接口最大值 Hello作用: 1. 发现邻居 2. 对一些数据的协商 3. 保持邻居的Keeplive状态.选举DR ...
- libevent实现多线程
libevent并不是线程安全的,但这不代表libevent不支持多线程模式.前几天在微博上看到ruanyf发了条微博说到apache和nginx的并发模型,看到评论很多人都说不对于是自己又查了下,总 ...
- Centos 配置服务器
Centos 配置服务器 (配置服务器 除了Git Bash Here 还可以安装Xshell 网址:https://xshell.en.softonic.com/ Wincp 网址:https: ...
- 解决H5设置了line-height但并没有居中的问题
遇到的问题 明明设置了line-height = height,但在H5页面里文字并没有居中. 原因 因为line-height是两条基线之间的距离,当元素高度和font-size差距较大时,会出现不 ...
- 【剑指Offer】二叉树的下一个结点 解题报告(Python)
[剑指Offer]二叉树的下一个结点 解题报告(Python) 标签(空格分隔): 剑指Offer 题目地址:https://www.nowcoder.com/ta/coding-interviews ...