25、onmouseover 、onmouseout 与onmouseenter 、onmouseleave的区别

时间:2022-11-06 08:06:10
onmouseover、nmouseout:鼠标移动到自身时候会触发事件,同时移动到其子元素身上也会触发事件

onmouseenter、onmouseleave:鼠标移动到自身是会触发事件,但是移动到其子元素身上不会触发事件

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title></title>
<style type='text/css'>
*{ margin:0; padding:0;}
#box3{
height:100px;
background:red;
}
#box2{
padding:50px;
background:blue;
}
#box1{
padding:50px;
background:green;
}
</style>
</head>

<body>
<div id="box1">1
<div id="box2">2
<div id="box3">3</div>
</div>
</div>

<script type="text/javascript">
/*
onmouseover
onmouseout

onmouseenter
onmouseleave
*/

var oBox1 = document.getElementById('box1');
///*
/*
oBox1.onmouseover = function(){ //可以直接通过ID这样调用,但不提倡
console.log( 'over' );
};
*/
oBox1.onmouseout = function(){ //可以直接通过ID这样调用,但不提倡
console.log( 'out' );
};
//*/

/*
oBox1.onmouseenter = function(){ //可以直接通过ID这样调用,但不提倡
console.log( 'enter' );
};
oBox1.onmouseleave = function(){
console.log( 'leave' );
};
*/
</script>
</body>
</html>