The trigger () method in jquery triggers the specified event type of the selected element, but some problems may occur during use. let's take a look at the trigger () in jQuery () if you have any questions about how to trigger the hover event, I hope it will be helpful to you. 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:
The 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.