JavaScript Event Learning Chapter 5 Advanced Event registration Model

Source: Internet
Author: User

Both W3C and Microsoft are developing their own event registration models to replace the traditional Netscape model. Although I am not very familiar with Microsoft models, w3c is still good, except for the mouse positioning problem. However, only a small number of browsers are supported.
W3C
W3C DOM-level event specification noticed the problem of the traditional mode. It provides a good solution for you to bind multiple events to an element.
The key to the W3C event registration model is addEventListener (). You give him three parameters: the event type, the function to be executed, and a Boolean value (true or false). I will explain it later. Register the well-known doSomething () function to an onclick event of an element. You can do this:
Element. addEventListener ('click', doSomething, false)
The charm of this model is that we can add as many listeners as we want. If we use the examples in our previous traditional model, we can write them as follows:
Element. addEventListener ('click', startDragDrop, false)
Element. addEventListener ('click', spyOnUser, false)
When a user clicks an element, both event handlers are executed. Note that the W3C standard cannot determine which event is executed first. Therefore, you cannot think that startDragDrop () is executed before spyOnUser.
Removing the event handler is also very simple, just use removeEventListener. Therefore:
Element. removeEventListener ('click', spyOnUser, false)
The second event handler will be removed, but the first handler will not change. A very beautiful program completely solves the problems in the traditional mode.

Anonymous Functions
In W3C mode, you can still use anonymous functions:
Copy codeThe Code is as follows:
Element. addEventListener ('click', function (){
This. style. backgroundColor = '# cc0000'
}, False)

True or false
True or false is the last parameter of addEventListener, which means that you want your function to be executed in the capture or bubble phase. If you are not sure, use false (bubble stage ).
This
In JavaScript, The this keyword usually refers to the function owner. If this points to the HTML element of the event, everything is so beautiful that you can do a lot of things very easily.
Unfortunately, although this is very powerful, it is still difficult to use it if you do not know exactly how it works. I will discuss this in detail in another place.
In the w3c model, he operates the same way as in the traditional mode: he represents the HTML elements that are currently processing events.
Copy codeThe Code is as follows:
Element. addEventListener ('click', doSomething, false );
Another_element.addEventListener ('click', doSomething, false );
Function doSomething (){
This. style. backgroundColor = '# cc0000 ';
}

If you register doSomething () on the click operation of any HTML element, the background of the element becomes red when you click it.

Which event handler is registered?
One problem with the W3C event registration mode is that you do not know which event handlers are registered for an element. In the traditional mode, you can:
Alert (element. onclick)
You can see which functions are registered. undefined means that no function is registered in this event. Only the W3C added an eventListenerList in the latest DOM Level 3 event to store the registered event handler. Because it is too new, there is little browser support. However, the problem has been solved.
Fortunately, removeEventListener () does not return errors because you have not registered an event of an element, so you do not have to worry about using removeEventLister ().
Microsoft
Microsoft also has an event registration model. It is similar to W3C but has a serious defect.
Register an event handler and attach it to an element:
Element. attachEvent ('onclick', doSomething)
Alternatively, you need two Event Handlers:
Element. attachEvent ('onclick', startDragDrop)
Element. attachEvent ('onclick', spyOnUser)
It is also very easy to remove one:
Element. detachEvent ('onclick', spyOnUser)

Defects
Compared with W3C, Microsoft has two serious problems:
Events are always bubbling and there is no possibility of being captured.
The event handler is referenced rather than copied, so the this keyword always points to the window and is not used at all.
The result of these two problems is that if an event is bubbling, you may not know which element is processing the event. I will explain in detail in the subsequent event sequence chapter.
Moreover, Microsoft's standards are only supported by IE and cannot be used across browsers. Even if you write a script for a windows browser, it is best not to use it because the bubble issue will make things unmanageable.
Continue
If you want to continue learning, please refer to the next chapter.
Address: http://www.quirksmode.org/js/events_advanced.html
My twitter: @ rehawk

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.