开发者

Confusion with jQuery mouseout event

开发者 https://www.devze.com 2022-12-25 09:13 出处:网络
My HTML: <div id="parent"> <div id="child">cx</div> </div> When I use jQuery

My HTML:

<div id="parent">
    <div id="child">cx</div>
</div>

When I use jQuery

$('#parent').mouseout(function(){
    //something here
});

I wonder why when my mouse enter the chi开发者_JAVA百科ld div the function fires. I'm still inside parent div. I want that mouseout function to fire only when I leave the parent div not when I'm on any child div.

http://jsbin.com/esiju/ << example

Cheers


This is what the mouseleave event is for.

$('#parent').mouseleave(function(){
//something here
});

http://api.jquery.com/mouseleave/


.mouseleave works perfectly here:

$("#parent").mouseleave(function(){
    //Enter stuff that should happen when mouse leaves 
    //the boundaries of the parent element
    // NOT including the children
});

.mouseout fires on mousing over child elements!


There seems to be a bit of difference between the mouseout and mouseover events. jimyi has the correct solution for your problem, I just wanted to include some additional links for completeness.

  • quirksmode
  • Quick example of mouseout and mouseleave
  • What looks like a much more in depth look at the difference between mouseout and mouseleave
0

精彩评论

暂无评论...
验证码 换一张
取 消