Home >Web Front-end >JS Tutorial >About two methods of obtaining events under Firefox and IE_javascript skills

About two methods of obtaining events under Firefox and IE_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:45:081064browse

Colleagues often ask me how to get events in Firefox. Most of them want to get the function of event.keyCode. There are two methods
The first method:

Copy code The code is as follows:

function a(e){
e=e||window.event;
alert(e.keyCode);
}

ie browser calls as follows
Copy code The code is as follows:



firefox calls the following
Copy code The code is as follows:



In this way, the call can be successful
This method requires a parameter in Firefox, which is not very good. Here is the second method
The second method:
Copy code The code is as follows:

function a(){
e=arguments.callee.caller.arguments[0 ] || window.event;
alert(e.keyCode);
}

Both ie and firefox call it as follows
Copy code The code is as follows:



Here is an explanation arguments.callee.caller.arguments[0],
A simple example is as follows:
Copy code The code is as follows:

function a(){
b();
}
function b(){
alert(b === arguments.callee)
alert(b. caller === a)
alert(arguments.callee.caller === a)
}
a();

The above example will output 3 trues , indicating the relationship between function b and function a when a() is called.
arguments.callee refers to the current function body
arguments.callee.caller is the superior function of the current function
So when onclick="a()" is executed, arguments.callee is a(), arguments .callee.caller is function onclick
The first function of onclick is event, which is arguments.callee.caller.arguments[0].
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