JavaScript Event Model Example analysis _javascript techniques

Source: Internet
Author: User

This example describes the use of the JavaScript event model. Share to everyone for your reference. The specific analysis is as follows:

One, event model

Bubbling event (Bubbling): The event is passed up from the leaf node to the root node along the ancestor node
Capture Event (capturing): The topmost element of the DOM tree to the most precise element, contrary to the bubbling event
DOM Standard event Model: The DOM standard supports both bubbling events and capture events, which can be said to be a combination of the two, first capturing, and then bubbling

Second, the event object

The event object is an attribute of window in IE Explorer, and in the DOM standard the event must be passed as a unique parameter to the handler function

To obtain a compatible event object:

function (event) { 
  //event is a parameter incoming handler function as a DOM standard 
 event = event? Event:window.event; 
} 

In IE, the object of the event is contained in the srcelement of the events, and in the DOM standard, the object is contained in the target attribute
Get the element that the compatible event object points to:

var target =event.srcelement? Event.srcElement:event.target;

The prerequisite is to ensure that the event object has been properly acquired

Third, event listeners

IE, registered listeners in reverse order, that is, after the registration of the first execution

Element.attachevent (' onclick ', observer); Register Listener
element.detachevent (' onclick ', observer)//Remove Listener

The first parameter is the event name and the second is the callback handler function

Under the DOM standard:

Element.addeventlistener (' click ', observer,usecapture) 
element.removeeventlistener (' Click ', Observer, Usecapture)

The first argument is the event name, there is no prefix on, the second parameter is a callback handler, and the third parameter indicates whether the callback function is called in the capture phase or the bubbling phase, and the default true is the capture phase

Iv. Event Delivery

To cancel the browser's event delivery in a compatible manner

function Somehandler (event) { 
  event = Event | | window.event; 
  if (event.stoppropagation)//dom standard 
  event.stoppropagation (); 
  else 
  event.cancelbubble = true;//ie Standard 
} 

Cancels the default processing after event passing

function Somehandler (event) { 
  event = Event | | window.event; 
  if (Event.preventdefault)//dom standard 
  event. Preventdefault (); 
  else 
  Event.returnvalue = true;//ie Standard 
}

I hope this article will help you with your JavaScript programming.

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.