Solution for jquery trigger function execution twice, jquerytrigger
This example describes how to execute the jquery trigger function twice. We will share this with you for your reference. The details are as follows:
I. The problem is as follows:
The following code is available:
Functions:
$('#old').bind("click", function(){$("input").trigger("focus");});
When Firefox is triggered only once, a focus is output;
However, it is triggered twice in ie, that is, two focuses are output simultaneously;
Ii. solution:
First, analyze trigger and triggerHandler. TriggerHandler does not trigger the default events of the browser, and does not generate event bubbles (for other differences, see the jQuery documentation ). Ticket about this bug. Commit for this question. JQuery implements an event object to solve the differences between browsers. However, due to the existence of non-standard events such as mouseenter/mouseleave, jQuery introduced the special event subsystem to bring the native event back to the event queue of the simulated event. However, this system cannot solve all the problems, when trigger. during focus, IE will incorrectly execute two callbacks.
TriggerHandler is the solution to trigger this problem. However, when triggerHandler is used, you will find that the input has no cursor focus effect.
Preliminary solution:
In addition to triggerHandler, another method is to add the following to the focus binding event:
event.preventDefault()
However, you find that this is not in line with our expectations, because the focus Event Callback is executed, but not even the focus effect.
Final Solution:
Since it is the link encapsulated by jQuery, we can use native events. See the demo. Native events are triggered on the left and triggerHandler on the right.
$('input')[0].focus();