Home > Web Front-end > JS Tutorial > Solution to the problem that trigger() in jquery cannot trigger the hover event_jquery

Solution to the problem that trigger() in jquery cannot trigger the hover event_jquery

WBOY
Release: 2016-05-16 16:00:25
Original
1189 people have browsed it

I was working on a project today and encountered a problem that I have never encountered before, so I made a note of it.

1. Explanation of trigger method

This is the official explanation:

Copy code 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] )

The eventType includes javascript built-in 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']);
Copy after login

is very powerful and is often used during page initialization.

2. Trigger meets hover

var $search=$('#header .search');
$search.find('li').hover(function()
{
 alert(1);
},function()
{
 alert(2);
});
$search.find('li').eq(0).trigger('hover');
Copy after login

Hover cannot be triggered. But:

var $search=$('#header .search');
$search.find('li').click(function()
{
 alert(1);
},function()
{
 alert(2);
});
$search.find('li').eq(0).trigger('click');
Copy after login

Triggering click is normal!

Solution:

var $search=$('#header .search');
$search.find('li').hover(function()
{
 alert(1);
},function()
{
 alert(2);
});
$search.find('li').eq(0).trigger('mouseenter');//hover修改为mouseenter/mouseleave/mouseover/mouseout
Copy after login

The same situation exists with jQuery.live(), but live is not recommended for use in versions after 1.7. Use on() instead.

The above is the entire content of this article, I hope you all like it.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template