Several concepts _javascript skills of JavaScript related events

Source: Internet
Author: User

The client JavaScript program uses an asynchronous event-driven programming model.

Several concepts of related events:

  Event Type: The string used to describe what type of event occurred;

  Event Target: the object in which the event occurred;

  event Handler Handler: a function that handles or responds to an event;

  Event object: An object that is related to a specific event and that contains details about the event;

  Event Propagation: the process by which the browser determines which object is to start its event handler;

To register an event handler:

1, set JavaScript object properties;

2. Set HTML Tag Properties

3, AddEventListener or attachevent (the latter is IE)

 function Addevent (target,type,handler) {
   if (target.addeventlistener) {
     Target.addeventlistener type, Handler,false);
   } else{
     target.attachevent (' On ' +type,function (event) {return Handler.call (target,event)});
   }
 

Three stages of event propagation:

1, occurs before the target processing function, is called the ' capture ' stage;

2, the object itself to deal with the event calls;

3, the bubbling stage of events;

In JavaScript, you can specify an event for an element in the following three ways :

1, in HTML, using the OnClick property
2, in JavaScript, using the OnClick property
3. In Javascipt, use the Addevenlistener () method

Comparison of three methods

(1) in the second to third method, you can pass in an event object to the function and read its corresponding property, and method one is not available.
(2) Preferred second to third, the first is not conducive to the separation of content and events, you can not use the event object related content.

Some grammatical details

(1) In the first method, the OnClick case is not relevant, but in the second method, you must use lowercase. Because HMTL is not sensitive to case, JS is case-sensitive.
(2) in the second to third method, there is no double quotation mark when specifying the function name, and the first one as an HTML property requires double quotes.
(3) The first method requires parentheses, and the second to third is not required.

 Onclick= "ClickHandler ()"
 document.getElementById ("Jsonclick"). onclick = ClickHandler2; 
 document.getElementById ("AddEventListener"). AddEventListener ("click", ClickHandler2);

Complete code:

<! DOCTYPE html> 
 
 

The above mentioned is the entire content of this article, I hope you can enjoy.

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.