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:
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:
$ (' #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
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:
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:
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.