Three puzzles about DOM elements and events _ DOM

Source: Internet
Author: User
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:



......

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.