Distinguish between simulated click events and user click events in JQuery

Source: Internet
Author: User

With JQuery, We can customize event functions, as shown in

[Javascript]
$ ("# Foo"). click (function (e ){
// Do work
});
We can also simulate the occurrence of click events, such

[Javascript]
$ ("# Foo"). click ()
Sometimes we need to distinguish this simulated event from the real user event.

Method 1:

We can determine whether a user clicks an event by passing the parameter e. If a user clicks an event, the object e will have attributes such as clientX, clientY, pageX, and pageY, all of which are numbers. You can also check the originalEvent attribute. The Code is as follows:

[Javascript]
$ ("# Foo"). click (function (e ){
If (e. hasOwnProperty ('ininalevent '))
// Probably a real click.
Else
// Probably a fake click.
});

Method 2:

You can also specify additional parameters when defining the event function. For details, see trigger api.

[Javascript]
$ ("# Foo"). click (function (e, from ){
If (from = null) from = 'user ';
// Rest of your code
});
 
$ ('# Foo'). trigger ('click', ['trigger']);

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.