Event Processing in JavaScript (1)

Source: Internet
Author: User

The principle of JavaScript event processing mechanism is a reflection gained when reading chapter 2nd of JavaScript advanced programming (12th.

In the event processing program, this points to a problem that I am quite entangled in. This knowledge was first learned from PPK about this keyword. When I first came into contact with JavaScript, it was a traditional event registration ):

VaR BTN = Document. getelementbyid ('loginbtn '); BTN. onclick = function () {alert (this. ID );}

The object btn of the htmlelement type has an attribute of "On + event name". In this anonymous function expression assigned to it, this points to BTN. Why does this point to BTN?

The following is a general attribute with an anonymous function expression:

VaR BTN = Document. getelementbyid ('loginbtn '); BTN. evthandler = function () {alert (this. ID);} BTN. evthandler ();

The event (Click above) is interesting: You don't need to write the called code in the program, but it is like executing the evthandler function.

The traditional event processing mentioned in PPK talking about JS must not contain brackets, which is the reason:

Function () handlerfn {alert (this. ID);} BTN. onclick = handlerfn;
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.