Home > Web Front-end > JS Tutorial > body text

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

WBOY
Release: 2016-05-16 17:33:13
Original
1011 people have browsed it

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.
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template