当前位置:Gxlcms > JavaScript > 关于div的onmouseover事件失效的问题解决

关于div的onmouseover事件失效的问题解决

时间:2021-07-01 10:21:17 帮助过:18人阅读

想实现如下功能:div标签内有个<img>,鼠标经过div时想改变图片,我的做法是,div的onmouseover事件:替换掉div的innerHTML内容(换成新的<img>),onmouseout事件再换回之前的图片,但onmouseover事件执行了,onmouseout事件却没有执行,这是为何?网上查了下说是有关事件冒泡的东西,针对我的需求有何简单解决方案?多谢--

<!DOCTYPE html>
<head>
<meta charset="utf-8">
<title>test</title>
<style>
</style>
<script type="text/javascript">
function changeImg() {
var imageEle = document.getElementById("img");
imageEle.src = "2.jpg";
}
function changeImgAgain() {
var imageEle = document.getElementById("img");
imageEle.src = "1.jpg";
}
</script>
</head>
<body>
<div id="handler" onmouseover="changeImg()" onmouseout="changeImgAgain()" style="backgound: #333; width: 300px; height: 300px"></div>
<div><image id="img" src="1.jpg" /></div>
</body>

将mouseout换成mouseleave事件。

思路是没有问题的,贴出你的代码吧,是你的代码有问题

$('#divId').hover(
  function() {//mouseover
    $(this).find("image").attr("src","......");
      },
      fucntion(){//mouseout
    $(this).find("image").attr("src",".....");
     }
);

以上就是关于div的onmouseover事件失效的问题解决的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行