JavaScript notes 04--Events and callbacks

Source: Internet
Author: User

1, in the browser, most of the code is event-driven (Event-driven).

This is somewhat similar to the neural reflex in the organism.

For example, a button on a Google page,

When we "press" this button, we will jump out of the following screen.

Have you ever thought about how this process happened?

Obviously, when we press the button, there must be a "piece of code" called.

This "piece of code" is what we usually call a "callback function" (callback).

2, then how to achieve a function like this?

It is natural to think that since we are going to respond to a particular event, what we need to know is

    • "Did the particular thing happen?" ”。
    • "How should I respond?" ”

It is easy to think of a pseudo-code similar to the following:

        Event . AddListener ();         if (event_happened) {            run_some_codes ();        }

Translate into JavaScript code, as shown below

We need to listen to a particular DOM element and invoke a "callback function" to complete the response when something happens.

The response to writing other things is similar, just follow the above routines.

3. The following are some properties of the "callback function" parameter event

Target refers to the DOM object that is listening.

Content from: http://htmldog.com/guides/javascript/intermediate/events/

JavaScript notes 04--Events and callbacks

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.