Home > Web Front-end > JS Tutorial > Summary of event object acquisition methods tested under Google browser_javascript skills

Summary of event object acquisition methods tested under Google browser_javascript skills

WBOY
Release: 2016-05-16 17:17:58
Original
1136 people have browsed it

Introduction:

The Event object represents the state of the event, such as the element in which the event occurred, the state of the keyboard keys, the position of the mouse, and the state of the mouse button. Events are often used in conjunction with functions, which are not executed until the event occurs!
Getting method of Event object:

Method 1:

For example:

Copy code The code is as follows:

function demo1(e){

var e = e || window.event;

// This method can obtain the event without passing parameter e in IE and Google, but in Firefox, the event parameter

// must be passed in the event method to obtain the event object.

}



Method 2: A universal and convenient method (the convenience of this method is that there is no need to pass parameters)

For example:
Copy code The code is as follows:

function demo2(){

var e = arguments.callee.caller.arguments[0] || window.event;

/ /arguments.callee refers to the current function body

//arguments.callee.caller is the superior function of the current function

//So when onclick="demo2()" is executed, arguments .callee is demo2(), arguments.callee.caller is function onclick,
//The first function of onclick is event, which is arguments.callee.caller.arguments[0].

}



Detailed test summary:

One: The same universal method is to pass the parameter event (the actual parameter must be written as event and other names will not work).

Two: window.event is suitable for IE and Google browsers and Firefox does not support it.

Three: arguments.callee.caller.arguments[0] is only supported by Firefox and Google. IE does not support this acquisition method.

From the above test results, it can be seen that Google Chrome supports the above various acquisition methods and is the most compatible browser.
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