• javascript----mouseover和mouseenter的区别


    1、代码(mouseover)

    <!DOCTYPE html>
    <html>
    	<head>
    		<meta charset="UTF-8">
    		<title></title>
    		<style type="text/css">
    			.div1{
    				 600px;
    				height: 600px;
    				background: red;
    			}
    			div{
    				padding: 20px;
    				
    			}
    			div div{
    				background-color: white;
    			}
    			div div div{
    				background-color: blue;
    			}
    		</style>
    	</head>
    	<body>
    		<div class="div1" id = 'div1'>
    			<div>
    				<div></div>
    			</div>
    			
    		</div>
    		<script>
    			var div1 = document.getElementById("div1");
    			div1.addEventListener("mouseover",function(){
    				 var r = parseInt(Math.random()*255);
    				 var g = parseInt(Math.random()*255);
    				 var b = parseInt(Math.random()*255);
    				 var s = "background:rgb("+r+","+g+","+b+")";
    				 
    				 console.log(s);
    				  
    				 div1.style = s;
    				 
    			})
    		</script>
    	</body>
    </html>
    

      

    2、效果(mouseover)

     3、总结

    mouseover会给子节点也绑定事件,传说中的冒泡事件,所以出现鼠标移动过程中触发三次事件

    1、代码(mouseenter)

    <!DOCTYPE html>
    <html>
    	<head>
    		<meta charset="UTF-8">
    		<title></title>
    		<style type="text/css">
    			.div1{
    				 600px;
    				height: 600px;
    				background: red;
    			}
    			div{
    				padding: 20px;
    				
    			}
    			div div{
    				background-color: white;
    			}
    			div div div{
    				background-color: blue;
    			}
    		</style>
    	</head>
    	<body>
    		<div class="div1" id = 'div1'>
    			<div>
    				<div></div>
    			</div>
    			
    		</div>
    		<script>
    			var div1 = document.getElementById("div1");
    			div1.addEventListener("mouseenter",function(){
    				 var r = parseInt(Math.random()*255);
    				 var g = parseInt(Math.random()*255);
    				 var b = parseInt(Math.random()*255);
    				 var s = "background:rgb("+r+","+g+","+b+")";
    				 
    				 console.log(s);
    				  
    				 div1.style = s;
    				 
    			})
    		</script>
    	</body>
    </html>
    
    
    
    <!--
    <!DOCTYPE html>
    <html>
    	<head>
    		<meta charset="UTF-8">
    		<title></title>
    		<style type="text/css">
    			.div1{
    				 600px;
    				height: 600px;
    				background: red;
    			}
    			div{
    				padding: 20px;
    				
    			}
    			div div{
    				background-color: white;
    			}
    			div div div{
    				background-color: blue;
    			}
    		</style>
    	</head>
    	<body>
    		<div class="div1" id = 'div1'>
    			<div>
    				<div></div>
    			</div>
    			
    		</div>
    		<script>
    			var div1 = document.getElementById("div1");
    			div1.addEventListener("mouseover",function(){
    				 var r = parseInt(Math.random()*255);
    				 var g = parseInt(Math.random()*255);
    				 var b = parseInt(Math.random()*255);
    				 var s = "background:rgb("+r+","+g+","+b+")";
    				 
    				 console.log(s);
    				  
    				 div1.style = s;
    				 
    			})
    		</script>
    	</body>
    </html>
    -->
    

      

    2、效果(mouseenter)

    3、总结

    mouseenter不是给子节点也绑定事件,也就是说,事件没有冒泡,所以在鼠标移动过程中只触发一次事件

  • 相关阅读:
    react typescript 子组件调用父组件
    Mongodb query查询
    CentOS虚拟机不能联网状况下yum方式从本地安装软件包(转载的)
    CentOS6.5 mini开启网络
    MySQL的InnoDB表如何设计主键索引-转自淘宝MySQL经典案例
    linux下安装mysql-community后起不来
    Eclipse 快捷键
    maven下载jta失败,自己本地安装jta库
    spring配置文件中id与name
    @autowired和@resource的区别
  • 原文地址:https://www.cnblogs.com/SunlikeLWL/p/7284327.html
Copyright © 2020-2023  润新知