onmouseout函数工作不正常

onmouseout function not working properly

本文关键字:不正常 工作 函数 onmouseout      更新时间:2023-09-26

我在用onmouseout函数编写的div中有一个类似缩略图的小图像。

<div onmouseout="chng()">
    <!-- Image Here -->
</div>

函数chng定义为:

function chng(){
  alert('Hi');
}

我的问题是,当我移动鼠标指针一点点时,它就会发出警报!

我想要的是,只有当鼠标指针完全在div之外时才会发出警报。

jQuery可以帮助:
<div id="my_div">image here</div>
<script>
$(function(){
    $("#my_div").hover(function(){
        // Put here some action for onMouseOver
    }, function(){
        // Put here some action for onMouseOut
    });
});
</script>

甚至:

<div id="my_div">image here</div>
<script>
$( "#my_div" ).mouseout(function() {
    // Put here some action for onMouseOut
});
</script>

<html>
  <script language="javascript">
    function chng()
    {
        alert("mouseout");
     }
   </script>
  <body>
    <div onmouseout="chng()">
      //Image Here
      </div>
  </body>
 </html>