mouseover和mouseenter两个事件的区别
mouseover(鼠标覆盖)
mouseenter(鼠标进入)
二者的本质区别在于,mouseenter不会冒泡,简单的说,它不会被它本身的子元素的状态影响到.但是mouseover就会被它的子元素影响到,在触发子元素的时候,mouseover会冒泡触发它的父元素.(想要阻止mouseover的冒泡事件就用mouseenter)
共同点:当二者都没有子元素时,二者的行为是一致的,但是二者内部都包含子元素时,行为就不同了.
贴出代码:
-
<!DOCTYPE html>
-
<html>
-
-
<head>
-
<meta charset="UTF-8">
-
<title></title>
-
<script type="text/javascript" src="js/jquery.min.js"></script>
-
<style>
-
.div1,
-
.div2,
-
.div3,
-
.div4 {
-
width: 200px;
-
height: 200px;
-
}
-
-
.div1 {
-
background: darkcyan;
-
}
-
-
.div2 {
-
background: violet;
-
}
-
-
.div3 {
-
background: darkgray;
-
}
-
-
.div4 {
-
background: darkgreen;
-
}
-
-
p {
-
width: 110px;
-
height: 100px;
-
background: aquamarine;
-
margin: 0 auto;
-
}
-
</style>
-
<script>
-
x = 0;
-
y = 0;
-
z = 0;
-
w = 0;
-
-
$(function() {
-
$(".div1").on("mouseenter", function() {
-
$("#s1").text(x += 1)
-
})
-
$(".div2").on("mouseover", function() {
-
$("#s2").text(y += 1)
-
})
-
$(".div2").on("mouseover", function() {
-
$("#s2").text(y += 1)
-
})
-
$(".div3").on("mouseenter", function() {
-
$("#s3").text(z += 1)
-
})
-
$(".div4").on("mouseover", function() {
-
-
$("#s4").text(w += 1)
-
})
-
})
-
</script>
-
</head>
-
-
<body>
-
<div class="div1">
-
div1没有子元素(mouseenter)
-
</div>
-
<span id="s1"></span>
-
<div class="div2">
-
div2没有子元素(mouseover)
-
</div>
-
<span id="s2"></span>
-
<div class="div3">
-
<p id="p1"> div3有子元素p(mouseenter)</p>
-
</div>
-
<span id="s3"></span>
-
<div class="div4">
-
<p id="p2">div4有子元素p(mouseover)</p>
-
</div>
-
<span id="s4"></span>
-
</body>
-
-
</html>
先测试一下div1和div2都是没有子元素的情况,看下面动图
可以看到当div没有子元素的时候,两者在鼠标覆盖或者进入的时候行为一样.
接下来看一下有子元素的div3和div4,如下动图
可以看到在有子元素的div3在mouseenter也不会触发div3
但是div4就会被在它的子元素被覆盖的时候被触发了,也就是产生了冒泡
最后注意:mouseenter就是在想要阻止冒泡事件发生的时候使用
把代码复制复制粘贴下来自己测试一下就明白了,不过注意: jquery需要的包你需要引入,不然是没有效果的.
代码下载链接: 代码链接
原文:https://blog.csdn.net/lplife/article/details/80436623
mouseover和mouseenter两个事件的区别的更多相关文章
- jQuery-使用hover(fn,fn)函数监听mouseover和mouseout两个事件
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- jQuery mouseover与mouseenter,mouseout与mouseleave的区别
mouseover与mouseenter 不论鼠标指针穿过被选元素或其子元素,都会触发 mouseover 事件. 只有在鼠标指针穿过被选元素时,才会触发 mouseenter 事件. mouseou ...
- mouseover与mouseenter,mouseout与mouseleave的区别
mouseover与mouseenter 不论鼠标指针穿过被选元素或其子元素,都会触发 mouseover 事件.只有在鼠标指针穿过被选元素时,才会触发 mouseenter 事件. mouseout ...
- 曾经跳过的坑----jQuery mouseover与mouseenter,mouseout与mouseleave的区别
mouseover与mouseenter 不论鼠标指针穿过被选元素或其子元素,都会触发 mouseover 事件. 只有在鼠标指针穿过被选元素时,才会触发 mouseenter 事件. mouseou ...
- jQuery里的mouseover与mouseenter事件类型区别
JQ里面有mouseover和mouseenter 2个事件类型干着差不多的活,用不好经常出现些小问题. 今天我解释一下原理: 事件类型翻译: mouseover 鼠标移上 mouseenter 鼠 ...
- mouseover和mouseenter的区别
mouseover和mouseenter都是鼠标事件. mouseover事件,当鼠标穿过被选元素的时候,若此元素有子元素,子元素也会被触发此事件.即是使用mouseover事件,会多次触发此元素. ...
- 事件处理& 事件委托& 区别mouseover与mouseenter
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 关于事件mouseover ,mouseout ,mouseenter,mouseleave的区别
轮播中大多会选择mouseover和mouseout 这个时候是没有任何问题的 但当遇到有css3动画的时候,会发现移入移出过快 动画还没加载完成就需要执行下一个动画,完了动画样式就错乱了. 这时候 ...
- 一个例子说明mouseover事件与mouseenter事件的区别
<html> <head> <meta charset="UTF-8"> <title>haha</title> < ...
随机推荐
- thymeleaf和freemarker比较
http://freemarker.cn/archives/168.html https://www.zhihu.com/question/64039553/answer/215942472 http ...
- Xamarin.FormsShell基础教程(5)Shell项目内容新建页面
Xamarin.FormsShell基础教程(5)Shell项目内容新建页面 轻拍内容列表页面中的Add按钮后,进入内容新建页面,如图1.4和图1.5所示.在该页面中,用户可以设置新建内容的标题和具体 ...
- Error-Javascript:错误:页面文档类型(DOCTYPE)未声明!
ylbtech-Error-Javascript:错误:页面文档类型(DOCTYPE)未声明! 1.返回顶部 1. HTML1300: 进行了导航.文件: TransferNote.aspxHTML1 ...
- js代码突然在花括号回车自动多加了一个大括号
js代码突然在花括号回车自动多加了一个大括号. 检查发现,前面有一个行带有左括号的注释行.
- WPF Button LinkButton 绑定多个值 Template
效果如下: 代码如下: <Button Click="InventoryDetail_OnClick" Template="{StaticResource Inve ...
- python初级(302) 5 列表
一.复习 1.函数的创建 1) def关键字 2) 函数名及后面跟随的括号 3)冒号加上函数体 2.函数的调用 函数名及后面跟随的括号 3.带一个,二个,三个参数的函数及调用 二.列表 family ...
- Java 待学习知识
Java 工厂模式和策略模式 Java 面向对象与面向接口的设计模式 Java 六大设计原则 - 单一职责原则 设计模式之禅 大话设计模式
- 工控随笔_23_WinCC报Application CCArchiveConnMon.exe could not establish a connection to the Win CC license server
西门子的软件都是比较庞大的,安装一个软件要很长时间,而且安装的过程中很容易因为系统兼容性问题导致不能成功安装的情况, 而有时还会出现安装成功,但是不能正常使用的情况.这个时候就很麻烦,一般除了重装系统 ...
- 1-2docker-基本的使用
1.Docker 官⽅提供了⼀个公共的镜像仓库 https://hub.docker.com 2.获取镜像 docker pull [选项] [Docker Registry 地址[:端⼝]/]仓库名 ...
- Error: Error occured while starting App. Original error: Activity used to start app doesn't exist or cannot be launched! Make sure it exists and is a launchable activity
Error: Error occured while starting App. Original error: Activity used to start app doesn't exist or ...