ChatGPT解决这个技术问题 Extra ChatGPT

What is the difference between the mouseover and mouseenter events?

I have always used the mouseover event, but while reading the jQuery documentation I found mouseenter. They seem to function exactly the same.

Is there a difference between the two, and if so when should I use them?
(Also applies for mouseout vs mouseleave).


f
flob

You can try out the following example from the jQuery doc page. It's a nice little, interactive demo that makes it very clear and you can actually see for yourself.

var i = 0; $("div.overout") .mouseover(function() { i += 1; $(this).find("span").text("mouse over x " + i); }) .mouseout(function() { $(this).find("span").text("mouse out "); }); var n = 0; $("div.enterleave") .mouseenter(function() { n += 1; $(this).find("span").text("mouse enter x " + n); }) .mouseleave(function() { $(this).find("span").text("mouse leave"); }); div.out { width: 40%; height: 120px; margin: 0 15px; background-color: #d6edfc; float: left; } div.in { width: 60%; height: 60%; background-color: #fc0; margin: 10px auto; } p { line-height: 1em; margin: 0; padding: 0; }

move your mouse
move your mouse

In short, you'll notice that a mouse over event occurs on an element when you are over it - coming from either its child OR parent element, but a mouse enter event only occurs when the mouse moves from outside this element to this element.

Or as the mouseover() docs put it:

[.mouseover()] can cause many headaches due to event bubbling. For instance, when the mouse pointer moves over the Inner element in this example, a mouseover event will be sent to that, then trickle up to Outer. This can trigger our bound mouseover handler at inopportune times. See the discussion for .mouseenter() for a useful alternative.


It's not true that mouseenter "only occurs when the mouse moves from the parent element to the element". The event occurs when the mouse changes from being outside the element to inside it. It doesn't matter which element the mouse came from. It's true that the mouse will often come from the parent, but not always. Eg, if the parent has no padding or border, then the mouse could enter straight from the grandparent, and mouseenter will still fire. In fact, it can even enter the element from outside the viewport (if the element is right at the edge) and the event still fires.
DEMO is best explanation ;)
for real, just play around with the demo.
J
Joseph

Mouseenter and mouseleave do not react to event bubbling, while mouseover and mouseout do.

Here's an article that describes the behavior.


This explains it perfectly: bl.ocks.org/mbostock/5247027 mouseover fires every time it is fired from within the container, because of event bubbling.
P
Peter Bailey

As is often true with questions like these, Quirksmode has the best answer.

I would imagine that, because one of jQuery's goals is to make things browser agnostic, that using either event name will trigger the same behavior. Edit: thanks to other posts, I now see this is not the case


n
naveed
$(document).ready(function() {
$("#outer_mouseover").bind
("Mouse Over Mouse Out",function(event){
console.log(event.type," :: ",this.id);})
$("#outer_mouseenter").bind
("Mouse enter Mouse leave",function(event){
console.log(event.type," :: ",this.id);})
 });


if your element has no child elements, but if the element does have children, then the pairs behave quite differently. In a nutshell, if you pass your mouse into an element and then into its child, mouseover/mouseout would both fire, whereas only mouseenter would fire since your mouse is still technically within the element.
I believe the event names here are wrong.