返回值:jQuerymouseout(handler(eventObject))

为 JavaScript 的 "mouseout" 事件绑定一个处理函数,或者触发元素上的该事件。(支持事件冒泡)

带有参数的该方法是 .bind('mouseout', handler) 的快捷方式。不带参数的该方法是 .trigger('mouseout') 的快捷方式。

当鼠标指针移出元素时,就会触发 mouseout 事件。任何 HTML 元素都可以接收该事件。

举例来说,请看下面的HTML:

<div id="outer">
  Outer
  <div id="inner">
    Inner
  </div>
</div>
<div id="other">
  Trigger the handler
</div>
<div id="log"></div>

该事件可以绑定在任何元素上:

$('#outer').mouseout(function() {
  $('#log').append('Handler for .mouseout() called.');
});

现在,当鼠标指针移出 Outer <div> 时,就会向 <div id="log"> 中追加信息。我们可以调用不带参数的 .mouseout() 方法,手动触发这个事件:

$('#other').click(function() {
  $('#outer').mouseout();
});

在代码执行后,如果你点击 Trigger the handler 将同样会追加上面的消息。

由于事件冒泡,该事件可能会产生诸多问题。例如上例中,当鼠标指针移出 Inner 元素时,会触发该元素上的 mouseout 事件,然后该事件又冒泡到 Outer 元素上。这就导致了不合时机的 mouseout 事件被调用了。更多相关信息,可以参阅 .mouseleave(),作为一种更好的替代方法。

示例:

当触发 mouseout 和 mouseleave 事件时,显示鼠标移出对象的次数。当鼠标移出绑定 mouseout 事件元素的子元素时,mouseout 事件同样会被触发。但是,只有在绑定 mouseleave 事件的元素上,将鼠标移出时,才会触发该事件。

<!DOCTYPE html>
<html>
<head>
<style>
div.out {
width:40%;
height:120px;
margin:0 15px;
background-color:#D6EDFC;
float:left;
}
div.in {
width:60%;
height:60%;
background-color:#FFCC00;
margin:10px auto;
}
p {
line-height:1em;
margin:0;
padding:0;
}
</style>
<script src="jquery.min.js"></script>
</head>
<body>


<div class="out overout"><p>move your mouse</p><div class="in overout"><p>move your mouse</p><p>0</p></div><p>0</p></div>

<div class="out enterleave"><p>move your mouse</p><div class="in enterleave"><p>move your mouse</p><p>0</p></div><p>0</p></div>



<script>


    var i = 0;
    $("div.overout").mouseout(function(){
      $("p:first",this).text("mouse out");
      $("p:last",this).text(++i);
    }).mouseover(function(){
      $("p:first",this).text("mouse over");
    });

    var n = 0;
    $("div.enterleave").bind("mouseenter",function(){
      $("p:first",this).text("mouse enter");
    }).bind("mouseleave",function(){
      $("p:first",this).text("mouse leave");
      $("p:last",this).text(++n);
    });



</script>
</body>
</html>

演示: