Home >Web Front-end >JS Tutorial >Example of processing hover event by jQuery's live() method_jquery

Example of processing hover event by jQuery's live() method_jquery

WBOY
WBOYOriginal
2016-05-16 16:57:51847browse

hover([over,]out)

A method that simulates hover events (the mouse moves over and out of an object)

When the mouse moves over a matching element, The first function specified will be triggered.

When the mouse moves out of this element, the specified second function will be triggered.

Copy code The code is as follows:

$('.myDiv').hover(function( ) {
doSomething...
}, function() {
doSomething...
});

The problem is that some elements such as menus are through AJAX It is loaded dynamically. When the hover method is executed, the

menu has not been loaded yet, so another method of jquery, live(),

.live() method can be used to return a Elements that have not been added to the DOM are valid due to the use of event delegation:

Event handlers bound to ancestor elements can respond to events triggered on descendants.

The event handler passed to .live() will not be bound to the element.

Instead, it will be treated as a special event handler and bound to the root node of the DOM tree. superior.
Copy code The code is as follows:

$('.myDiv').live('hover ',function(event){
if(event.type=='mouseenter'){
doSomething...
}else{
doSomething...
}
} )

Some jquery versions respond to mouseenter and mouseleave
Some respond to mouseover and mouseout
To be verified...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn