JavaScript: The Definitive Guide, Sixth Editio javaScript权威指南(第6版) pdf 文字版-文字版, javascript电子书, 和javascript 有关的电子书:

17.3.3 Event Handler Scope

Note that event handlers registered using this method cannot be removed, since the wrapper function passed to attachEvent() is not retained anywhere to be passed to detachEvent().

17.3.3 Event Handler Scope

Like all JavaScript functions, event handlers are lexically scoped. They are executed in the scope in which they are defined, not the scope from which they are invoked, and they can access any local variables from that scope. (This is demonstrated in the addEvent() function above, for example.)

Event handlers registered as HTML attributes are a special case, however. They are converted into top-level functions that have access to global variables but not to any local variables. But, for historical reasons, they run with a modified scope chain. Event handlers defined by HTML attributes can use the properties of the target object, the containingobject (if there is one), and the Document object as if they are local variables. §17.2.2 shows how an event handler function is created from an HTML event handler attribute, and the code there approximates this modified scope chain using with statements.

17.3 Event Handler Invocation | 461

HTML attributes are not natural places to include long strings of code, and this modified scope chain allows helpful shortcuts. You can use tagName instead of this.tag Name. You can use getElementById instead of document.getElementById. And, for document elements that are inside a, you can refer to any other form element by ID, using zipcode, for example, instead of this.form.zipcode.

友情链接It题库(| 版权归yishouce.com所有| 友链等可联系|粤ICP备16001685号-1