I’m new to jQuery and am facing now a weird problem. I succeeded to narrow it down to the fact that a mouseenter event is called twice: once for the containing div (this was my intention) and again for elements within this div (not good). I tried to use return false and stopPropagation but it doesn’t seem to work.
Here’s the code:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<!-- JS files (order matters!) -->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.js"></script>
<script type="text/javascript">
$(function (){
$(".testDiv").hover(
function(e) /* IN */ {
$(this).data("htmlBackup", $(this).html());
$(this).html("TEST 123");
e.stopPropagation();
return false;
}, function(e) /* OUT */ {
$(this).html($(this).data("htmlBackup"));
e.stopPropagation();
return false;
});
});
</script>
<!-- this one works -->
<div class="testDiv" style="border: solid">ORIG HTML</div>
<br /> <br /> <br />
<!-- this doesn't work -->
<div class="testDiv" style="border: solid"> <p style="border: solid">ORIG HTML</p></div>
</body>
</html>
You can also see it here: http://jsfiddle.net/rFqyP/3/
Any help will be very much appreciated!
You can keep the code from getting stuck by using a flag, so that you can detect when you get double
mouseenterevents:http://jsfiddle.net/rFqyP/16/
This will however not solve the problem with the size difference. When you leave the element by moving out by the bottom border, it grows and causes a
mouseenterevent, which again changes the size so that the mouse is outside but without causing amouseleaveevent, leaving the element looking like the mouse is still hovering it.Remving the border from the
pelements solves the problem completely, without a need for a flag, as it’s the border that is causing the size difference.