An example of how jQuery's live of method handles hover events

  • 2020-03-30 02:08:36
  • OfStack

Hover over,] out)

A method that mimics a hover event (moving the mouse over and out of an object)

When the mouse moves over a matched element, the first specified function is triggered.

When the mouse moves over the element, the specified second function is triggered.
 
$('.myDiv').hover(function() { 
doSomething... 
}, function() { 
doSomething... 
}); 

The problem is that some elements such as menus are loaded dynamically with AJAX, when the hover method is executed

The menu isn't loaded yet, so I'm going to use another jquery method called live()

The.live() method works for an element that has not been added to the DOM because of the use of event delegates:

An event handler bound to an ancestor element can respond to an event that fires on a descendant.

The event handler passed to.live() is not bound to the element,

Instead, it is bound to the root node of the DOM tree as a special event handler.
 
$('.myDiv').live('hover',function(event){ 
if(event.type=='mouseenter'){ 
doSomething... 
}else{ 
doSomething... 
} 
}) 

Some jquery versions respond to mouseenter and mouseleave
Some are mouseover and mouseout
Disputed...

Related articles: