代码示例:(标识:event_onmouseenter_addeventlistener)
<!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>
运行结果: