In jquery, trigger () cannot trigger the hover event. jqueryhover
I encountered a problem when I was working on a project today, but I did not know it before.
1. trigger method explanation
The official explanation is as follows:
Copy codeThe Code is as follows:
Description: Execute all handlers and behaviors attached to the matched elements for the given event type.
Usage:
. Trigger (eventType [, extraParameters])
EventType includes javascript built-in events, events added by jQuery, and custom events. For example:
$('#foo').bind('click', function(){ alert($(this).text());});$('#foo').trigger('click');$('#foo').bind('custom', function(event, param1, param2){ alert(param1 + "\n" + param2);});$('#foo').trigger('custom', ['Custom', 'Event']);
Very powerful, often used for page initialization.
2. trigger encounters hover
var $search=$('#header .search');$search.find('li').hover(function(){ alert(1);},function(){ alert(2);});$search.find('li').eq(0).trigger('hover');
The hover cannot be triggered. However:
var $search=$('#header .search');$search.find('li').click(function(){ alert(1);},function(){ alert(2);});$search.find('li').eq(0).trigger('click');
A normal click is triggered!
Solution:
Var $ search = $ ('# header. search'); $ search. find ('lil '). hover (function () {alert (1) ;}, function () {alert (2) ;}); $ search. find ('lil '). eq (0 ). trigger ('mouseenter'); // hover: mouseenter/mouseleave/mouseover/mouseout
The same situation exists in jQuery. live (). However, live is not recommended for Versions later than 1.7, instead of on.
The above is all the content of this article. I hope you will like it.