Home  >  Article  >  Web Front-end  >  Let’s talk about two things: 1. A brief exploration of the usage of this pointer. 2. The difference in event processing between ie’s attachEvent and firefox’s addEventListener_javascript skills

Let’s talk about two things: 1. A brief exploration of the usage of this pointer. 2. The difference in event processing between ie’s attachEvent and firefox’s addEventListener_javascript skills

WBOY
WBOYOriginal
2016-05-16 19:15:001005browse

The first thing.
This clock is a special clock in JavaScript language. When the code is running, it points to the current object that calls this statement.
If it is an event binding function, it points to the bound function. The element itself.


It is particularly worth noting that when multiple objects are nested, this points to the object that recently called it
obj1={
obj2: {
                                                                                                                                                                                                                                                                                                  .
                                                                                                                                                             >The second thing:
Without further ado, let’s serve the side dishes first.




I really don’t understand why ie does this, it makes people very depressed, why What about pointing this to window?
Solution:
1. Don’t use attachEvent when binding events. Poor me, I just used the Event.Observe method of prototype.js at that time
This way element.onclick= function..... In this way, in both browsers, this finger points to element
2. In the processing function gorush, use the getEvent() method to uniformly obtain the event, and then use evt.srcElement || evt.target to obtain element object

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn