The trigger () method in jquery triggers the specified event type for the selected element, but there are some problems in using it, so let's take a look at the trigger () triggering the hover event in jquery and hope it will help you.
Today to do a project, encountered a problem, is not encountered before, to write a note on this.
1, Trigger method explanation
This is the official explanation:
Copy code code as follows:
Description:execute all handlers and behaviors attached to the matched of the for the Elements event type.
Usage:
. Trigger (EventType [, Extraparameters])
Where EventType contains JavaScript-built events, jquery-added events, and custom events. For example:
?
1 2 3 4 5 6 7 8 9 10 |
$ (' #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 when page initialization is used.
2, Trigger met hover
?
1 2 3 4 5 6 7 8 9 |
var $search =$ (' #header. Search '); $search. Find (' Li '). Hover (function () {alert (1);},function () {alert (2);}); $search. Find (' li ')-eq (0). Trigger (' hover '); |
Unable to trigger hover. But:
?
1 2 3 4 5 6 7 8 9 |
var $search =$ (' #header. Search '); $search. Find (' Li '). Click (function () {alert (1);},function () {alert (2);}); $search. Find (' li ')-eq (0). Trigger (' click '); |
Trigger Click Normal!
Solution:
?
1 2 3 4 5 6 7 8 9 |
var $search =$ (' #header. Search '); $search. Find (' Li '). Hover (function () {alert (1);},function () {alert (2);}); $search. Find (' li ')-eq (0). Trigger (' MouseEnter ');//hover modified to Mouseenter/mouseleave/mouseover/mouseout |
The same situation exists in jquery.live (), but live is not recommended for use on the version after 1.7, using on () instead.
The above mentioned is the entire content of this article, I hope you can enjoy.