Home >Web Front-end >JS Tutorial >Introduction to the use of event parameters in JavaScript event processors_Basic knowledge

Introduction to the use of event parameters in JavaScript event processors_Basic knowledge

WBOY
WBOYOriginal
2016-05-16 17:33:131044browse

In most browsers, when an event handler is triggered, an instance of the class named Event is passed into the handler as the first parameter. However, Internet Explorer, which has always been in the mainstream, behaves in its own way and saves the Event instance in a global attribute called event.

Copy code The code is as follows:

if (!event) event=window.event;

The above statement is used to detect whether the event parameter is undefined or null. If so, assign the event attribute of the window to it, thereby eliminating browser differences.

To obtain a reference to the target element, use the target attribute in standards-compliant browsers and the srcElement attribute in IE to handle this inconsistency through object detection
Copy code The code is as follows:

var target=(event.target) ? event.target : event.srcElement;

This statement checks whether the definition of event.target exists. If it exists, assign its value to the local variable target; otherwise, assign event.srcElement to target.
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