Event binding: There are five types of events:
1. bind to an elementThis is also a common example:
<Input type = "button" onclick = "doeventthing (event)">
In this way, we bind doeventthing to the button object and click this button to trigger the event.
2. Bind events to objects: This is also a common one, especially under ie4 +:
Document. getelementbyid ("divid"). onclick = doeventthing;
3. Use <script for> to bind eventsThis is only useful in ie4 + (BIND events for buttong1, and the logic writes events in the script block to specify how to trigger events ):
<SCRIPT event = "onclick" for = "button1">
// Script statements here
</SCRIPT>
4. Use the attachevent () method of ie5/Windows
5. Use the addeventlistener () method of W3C dom
Addeventlistener ("eventtype", listenerreference, captureflag );
The third parameter is a Boolean value indicating whether the node listens for events in the so-called capture mode in the Dom. For a typical event listener, the third parameter should be false ).
Prototype is compatible with IE and W3C when binding events as follows:
_ Observeandcache: function (element, name, observer, usecapture) {If (! This. observers) This. observers = []; If (element. addeventlistener) {// W3C Dom this. observers. push ([element, name, observer, usecapture]); element. addeventlistener (name, observer, usecapture);} else if (element. attachevent) {// ie5/Windows this. observers. push ([element, name, observer, usecapture]); element. attachevent ('on' + name, observer );}}
Aside from this. Observers. pust ([element, name, observer, usecapture]), we will be very clear about binding events 4 and 5. We know that the usecapture of this prototype method does not work in IE. It only works for W3C event processing mechanism.