In jquery, trigger () cannot trigger the hover event. jqueryhover

Source: Internet
Author: User

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.

Related Article

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.