小码哥的IT人生

onmouseenter 事件

JavaScript基础 2022-06-08 12:01:19小码哥的IT人生shichen

onmouseenter 事件

实例

将鼠标指针移动到图像上时执行 JavaScript:

<img onmouseenter="bigImg(this)" src="smiley.gif" alt="Smiley">

完整实例:

<!DOCTYPE html>
<html>
<body>
<img onmouseenter="bigImg(this)" onmouseleave="normalImg(this)" border="0" src="/i/photo/smile.gif" alt="Smiley" width="128" height="128">
<p>当用户将鼠标指针移动到图像上时会触发函数 bigImg()。</p>
<p>当鼠标指针移出图像时会触发函数 normalImg()。</p>
<script>
function bigImg(x) {
  x.style.height = "256px";
  x.style.width = "256px";
}
function normalImg(x) {
  x.style.height = "128px";
  x.style.width = "128px";
}
</script>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

页面下方有更多 TIY 实例。

定义和用法

onmouseenter 事件在鼠标指针移动到元素上时发生。

提示:此事件通常与 onmouseleave 事件一起使用,当鼠标指针移出元素时会发生该事件。

提示: onmouseenter 事件类似于 onmouseover 事件。唯一的区别是 onmouseenter 事件不会冒泡(不会向上级文档层次结构传播)。请参阅页面底部的更多实例。

浏览器支持

表中的数字注明了完全支持该事件的首个浏览器版本。

事件 Chrome IE Firefox Safari Opera
onmouseenter 30.0 5.5 支持 6.1 11.5

语法

在 HTML 中:

<element onmouseenter="myScript">

完整实例:

<!DOCTYPE html>
<html>
<body>
<p>本例演示如何将 "onmouseenter" 和 "onmouseleave" 事件分配给 h1 元素。</p>
<h1 id="demo" onmouseenter="mouseEnter()" onmouseleave="mouseLeave()">请把鼠标悬停在我上面</h1>
<script>
function mouseEnter() {
  document.getElementById("demo").style.color = "red";
}
function mouseLeave() {
  document.getElementById("demo").style.color = "black";
}
</script>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

在 JavaScript 中:

object.onmouseenter = function(){myScript};

完整实例:

<!DOCTYPE html>
<html>
<body>
<p>本例使用 HTML DOM 将 "onmouseenter" 和 "onmouseleave" 事件分配给 h1 元素。</p>
<h1 id="demo">请把鼠标悬停在我上面</h1>
<script>
document.getElementById("demo").onmouseenter = function() {mouseEnter()};
document.getElementById("demo").onmouseleave = function() {mouseLeave()};
function mouseEnter() {
  document.getElementById("demo").style.color = "red";
}
function mouseLeave() {
  document.getElementById("demo").style.color = "black";
}
</script>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

在 JavaScript 中,使用 addEventListener() 方法:

object.addEventListener("mouseenter", myScript);

完整实例:

<!DOCTYPE html>
<html>
<body>
<p>本例使用 addEventListener() 方法将 "mouseenter" 和 "mouseleave" 事件附加到 h1 元素。</p>
<h1 id="demo">请把鼠标悬停在我上面</h1>
<script>
document.getElementById("demo").addEventListener("mouseenter", mouseEnter);
document.getElementById("demo").addEventListener("mouseleave", mouseLeave);
function mouseEnter() {
  document.getElementById("demo").style.color = "red";
}
function mouseLeave() {
  document.getElementById("demo").style.color = "black";
}
</script>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

注释:Internet Explorer 8 或更早的版本不支持 addEventListener() 方法

技术细节

冒泡: 不支持
可取消: 不支持
事件类型: MouseEvent
支持的 HTML 标签: 所有 HTML 元素,除了:<base>, <bdo>, <br>, <head>, <html>, <iframe>, <meta>, <param>, <script>, <style> 以及 <title>
DOM 版本: Level 2 Events

更多实例

示例代码:

这个例子演示了 onmousemove、onmouseenter 和 mouseover 事件之间的区别:

<div onmousemove="myMoveFunction()">
  <p id="demo">I will demonstrate onmousemove!</p>
</div>
<div onmouseenter="myEnterFunction()">
  <p id="demo2">I will demonstrate onmouseenter!</p>
</div>
<div onmouseover="myOverFunction()">
  <p id="demo3">I will demonstrate onmouseover!</p>
</div>

完整实例:

<!DOCTYPE html>
<html>
<head>
<style>
div {
  width: 100px;
  height: 100px;
  border: 1px solid black;
  margin: 10px;
  float: left;
  padding: 30px;
  text-align: center;
  background-color: lightgray;
}
p {
  background-color: white;
}
</style>
</head>
<body>
<h3>本例演示了 onmousemove、onmouseleave 与 onmouseout 之间的区别。</h3>
<p>每次鼠标指针移动到 div 元素上时都会发生 onmousemove 事件。</p>
<p>mouseleave 事件仅在鼠标指针移出 div 元素时发生。</p>
<p>onmouseout 事件在鼠标指针移出 div 元素时发生,且当它离开子元素(p 和 span)。</p>
<div onmousemove="myMoveFunction()">
  <p>onmousemove: <br> <span id="demo">请把鼠标悬停在我上面!</span></p>
</div>
<div onmouseenter="myEnterFunction()">
  <p>onmouseenter: <br> <span id="demo2">请把鼠标悬停在我上面!</span></p>
</div>
<div onmouseover="myOverFunction()">
  <p>onmouseover: <br> <span id="demo3">请把鼠标悬停在我上面!</span></p>
</div>
<script>
var x = 0;
var y = 0;
var z = 0;
function myMoveFunction() {
  document.getElementById("demo").innerHTML = z+=1;
}
function myEnterFunction() {
  document.getElementById("demo2").innerHTML = x+=1;
}
function myOverFunction() {
  document.getElementById("demo3").innerHTML = y+=1;
}
</script>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

版权所有 © 小码哥的IT人生
Copyright © phpcodeweb All Rights Reserved
ICP备案号:苏ICP备17019232号-2  

苏公网安备 32030202000762号

© 2021-2024