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;