Solution for jquery trigger function execution twice, jquerytrigger

Source: Internet
Author: User

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();

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.