Home > Web Front-end > JS Tutorial > Detailed explanation of how the live() method handles hover events in jQuery

Detailed explanation of how the live() method handles hover events in jQuery

黄舟
Release: 2017-06-26 09:25:47
Original
1510 people have browsed it

hover([over,]out)

A simulated hover event (the mouse moves to a ObjectMethods on and out of this object

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

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

$('.myp').hover(function() {
	doSomething...	
}, function() {
	doSomething...	
});
Copy after login


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

the menu has not yet been loaded. It is loaded, so we need to use another method of jquery, live()

.live() method can be effective for an element that has not been added to the DOM because it is used Event delegate:

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

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

but will be treated as a special The event handling function is bound to the root node of the DOM tree.

$('.myp').live('hover',function(event){
	if(event.type=='mouseenter'){
		doSomething...	
	}else{
		doSomething...	
	}
})
Copy after login


Requires jQuery 1.4.3+
Because hover is not a standard event, it cannot be processed directly using live, so the following method is used instead, with the same effect.

$("table tr").live({
   mouseenter:
   function()
   {
      //todo
   },
   mouseleave:
   function()
   {
      //todo
   }
});
Copy after login

The above is the detailed content of Detailed explanation of how the live() method handles hover events in jQuery. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template