You can refer to the three puzzles about DOM elements and events to learn and use dom operations.
I. background knowledge
There are several ways to add events to DOM elements:
1. Hard Encoding
2. event listening
3. Event binding provided by the JS framework
1. hardcoded format: the Original Event format.
The code is similar to the following:
The Code is as follows:
Document. getElementById ('element _ id'). eventName = func ();
2. event listening mechanism. This method is based on the DOM event mechanism, which is divided into the standard DOM event model addEventListener and the IE event model attchEvent.
The code is similar to the following:
The Code is as follows:
Var addEventHandler = function (ele, evt, fn ){
If (ele. addEventListener ){
Ele. addEventListener (evt, fn, false );
}
Else
If (ele. attachEvent ){
Ele. attachEvent ('on' + evt, fn );
}
Else {
Ele ['on' + evt] = fn;
}
};
Var ele = document. getElementById ('element _ id ');
AddEventHandler (ele, 'eventname', function () {code ...});
3. Event binding mechanism provided by the JS framework. Here we use the jQuery framework.
The code is similar to the following:
The Code is as follows:
$ ('Element _ id'). bind ('eventname', function (event) {code ...});
$ ('Element _ id'). click (function () {code ...});
2. puzzles
The puzzle is as follows:
The Code is as follows:
......