Trigger () in jquery triggers hover event queries

Source: Internet
Author: User
Tags bind


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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.