Home  >  Article  >  Web Front-end  >  DOM basic tutorial event object_basic knowledge

DOM basic tutorial event object_basic knowledge

WBOY
WBOYOriginal
2016-05-16 16:18:551000browse

Events in browsers exist in the form of objects. Similarly, there are differences in obtaining event objects between IE browsers and standard DOM browsers. In the IE browser, the event object is an attribute event of the windows object. The access method usually uses the following method.

Copy code The code is as follows:

oP.onclick = function(){
var oEvent = window.event;
}

Although it is a window object property, the event object can only be accessed when the event occurs. After all event processing functions are executed, the object disappears.

The standard DOM stipulates that the event object must be passed to the event processing function as the only parameter. Once accessed in a browser like Firefox, the event object is usually used as a parameter, and the code is as follows:

Copy code The code is as follows:

oP.onclick = function(oEvent){
}

Therefore, in order to be compatible with both browsers, the following method is usually used

Copy code The code is as follows:

oP.onclick = function(oEvent){
if(window.event)oEvent = window.event;
}

After obtaining the event object, the browser can handle various events through its series of properties and methods, such as mouse events, keyboard events and browser events. Wait

The commonly used attributes and methods are listed below:

As can be seen from the above, the two types of browsers still have some similarities. For example, the type attribute is compatible with various browsers. It indicates the type of event obtained and returns something like "click" or "mousemove". class value.

This is useful for handling multiple types of events with the same function.

As follows: the same function handles multiple events.

Copy code The code is as follows: