概述 .mouseout( handler(eventObject) )
返回值:jQuery
描述:为 JavaScript 的 "mouseout" 事件绑定一个处理函数,或者触发元素上的该事件。(手册网注:支持事件冒泡)
这个方法的前两个用法是 .bind('mouseout', handler)
的快捷方式,第3个不带参数的用法是 .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>
元素上移出时,“Handler for .mousedown() called.”这个字符串将被添加到<div id="log">
。我们可以调用不带参数的 .mouseout()
方法,手动触发这个事件:
- $('#other').click(function() {
- $('#outer').mouseout();
- });
这些代码执行后,点击Trigger the handler 同样添加这个信息。
由于此事件类型冒泡,可能导致引起的很多头痛的问题。例如,在这个示例中当鼠标指针移出Inner元素,mouseout
事件将被发送到Inner元素,然后冒泡到Outer元素 。这可能会不合时宜的触发绑定的mouseout
处理函数。这可以用一个替代方法,见讨论. mouseleave ()
。
.mouseout()
方法只是作为.on( "mouseout", handler )
的一个速记写法,移除该事件可以使用.off(
"mouseout"
)
。
示例
当触发 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="http://code.jquery.com/jquery-latest.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>