Prevent onmouseout when hovering child element of the parent absolute div WITHOUT jQuery
function onMouseOut(event) {
//this is the original element the event handler was assigned to
var e = event.toElement || event.relatedTarget;
if (e.parentNode == this || e == this) {
return;
}
alert('MouseOut');
// handle mouse event here!
}
document.getElementById('parent').addEventListener('mouseout',onMouseOut,true);
I made a quick JsFiddle demo, with all the CSS and HTML needed, check it out...
EDIT FIXED link for cross-browser support http://jsfiddle.net/RH3tA/9/
NOTE that this only checks the immediate parent, if the parent div had nested children then you have to somehow traverse through the elements parents looking for the "Orginal element"
EDIT example for nested children
EDIT Fixed for hopefully cross-browser
function makeMouseOutFn(elem){
var list = traverseChildren(elem);
return function onMouseOut(event) {
var e = event.toElement || event.relatedTarget;
if (!!~list.indexOf(e)) {
return;
}
alert('MouseOut');
// handle mouse event here!
};
}
//using closure to cache all child elements
var parent = document.getElementById("parent");
parent.addEventListener('mouseout',makeMouseOutFn(parent),true);
//quick and dirty DFS children traversal,
function traverseChildren(elem){
var children = [];
var q = [];
q.push(elem);
while (q.length > 0) {
var elem = q.pop();
children.push(elem);
pushAll(elem.children);
}
function pushAll(elemArray){
for(var i=0; i < elemArray.length; i++) {
q.push(elemArray[i]);
}
}
return children;
}
And a new JSFiddle, EDIT updated link
JavaScript mouseover/mouseout issue with child element
you can use "mouseenter" and "mouseleave" events available in jquery, here is the below code,
$(document).ready(function () {
$("#parent").mouseenter(function () {
$("#child").show();
});
$("#parent").mouseleave(function () {
$("#child").hide();
});
});
above is to attach an event,
<div id="parent">
<img id="child" src="button.png" style="display:none;" />
</div>
Mouseover on child element, not on parent
You need to use mouseover and mouseout
$(element).mouseover(function (event) {
event.stopPropagation();
showHighlightContainer();
}).mouseout(function (event) {
event.stopPropagation();
hideHighlightContainer();
});
Demo: Fiddle
How to disable mouseout events triggered by child elements?
The question is a bit old, but I ran into this the other day.
The simplest way to do this with recent versions of jQuery is to use the mouseenter
and mouseleave
events rather than mouseover
and mouseout
.
You can test the behavior quickly with:
$(".myClass").on( {
'mouseenter':function() { console.log("enter"); },
'mouseleave':function() { console.log("leave"); }
});
How to prevent child element executing onmousedown event
Check event.target
to find out which element was actually clicked on.
var element = "#box";
document.querySelector(element).onmousedown = function(e) {
if (e.target.id !== "box_child_three") {
alert("triggered");
}
};
<div id="box">
<div id="box_child_one">one</div>
<div id="box_child_two">two</div>
<div id="box_child_three">three</div>
</div>
Prevent onmouseout from firing in Blazor when moving over a child element
This may not be right but couldn't you use @onmouseout:preventDefault
or @onmouseout:stopPropagation
on the child HTML elements?
I haven't tested of course, but these exist for all Blazor events: https://docs.microsoft.com/en-us/aspnet/core/blazor/components/event-handling?view=aspnetcore-5.0#prevent-default-actions
MouseEnter event does not fire when entering parent from child element
As Boris explained in his answer, there's 2 different events. To quote him :
mouseenter
andmouseleave
are triggered when you enter and leave a hierarchy of nodes, but not when you navigate that hierarchy's descendance.mouseover
andmouseout
are triggered when the mouse respectively enters and leaves a node's "exclusive" space, so you get a "out" when the mouse gets into a child node.
That being said, you need mouseover
and mouseout
since you need to trigger the mouse out when hovering a child.
$("*").on("mouseover", function(e) {
$(this).addClass("mouse_over");
});
$("*").on("mouseout", function(e) {
$(this).removeClass("mouse_over");
});
Next, you need to know that event bubble in the tree. So when you mouseover
on a child, the event is also propagated to the parents. That's why the parent is in hover state even when hovering a child.
To solve that, you need to use .stopPropagation()
on the event object.
$("*").on("mouseover", function(e) {
$(this).addClass("mouse_over");
e.stopPropagation();
});
$("*").on("mouseout", function(e) {
$(this).removeClass("mouse_over");
e.stopPropagation();
});
See it in action
Bonus
In the spirit of writing less code, you can also use that :
$("*").on("mouseover mouseout", function(e) {
$(this).toggleClass("mouse_over");
e.stopPropagation();
});
Related Topics
Dynamically Resizing Image-Maps and Images
How to Use CSS (And JavaScript) to Create a Blurred, "Frosted" Background
Forcing Web-Site to Show in Landscape Mode Only
How to Save a Base64 Image to User'S Disk Using JavaScript
Why Is ≪Marquee≫ Deprecated and What Is the Best Alternative
How to Display a JavaScript Var in HTML Body
Get Iframe'S Document, from JavaScript in Main Document
How to Automatically Reload a Web Page At a Certain Time
How to Get the Background Color of an HTML Element
How to Open a Link in New Tab (And Not New Window)
Does Schema.Org Markup Work If Markup Is Dynamically Built With JavaScript
How to Refer to JavaScript Variables Across Webpages in a Browser Session
Html5 Dragleave Fired When Hovering a Child Element
Select All Elements With a "Data-Xxx" Attribute Without Using Jquery
Positioning ≪Div≫ Element At Center of Screen
Pure JavaScript Method to Wrap Content in a Div
Chrome Counts Characters Wrong in Textarea With Maxlength Attribute