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