But there is no such object in Firefox, if there is a function nested call, you need to continue to pass the event, such as the following scene.
Copy Code code as follows:
<div style= "background-color:red; width:300px; height:300px, "onclick=" Test (event,this); "Id=" Panel ></div>
function Test (event,dom) {
Test1 (event);
}
function Test1 (event) {
Test2 (event);
}
function Test2 (event) {
alert (event.target.id);
}
The need to use event in the Test2 method is written in this way. If in some scenario, such as adding new features, need to modify the original Test2 method, need to access the event object, and the original Test2 method signature is Test2 (), no parameter event, then need to modify Test2 () Test2 (event) is very beautiful, Although JavaScript is such a modification, it is the overload of the method, but it also destroys the original method signature.
Is there a global variable such as window.event in Firefox to get an event?
Unfortunately, the Firefox object model is not available, but it can be achieved using workarounds. For example:
Copy Code code as follows:
function GetEvent (caller) {
if (document.all)
return window.event; For IE.
if (caller = null | | typeof (CALLER)!= "function")
return null;
while (Caller.caller!= null) {
caller = Caller.caller;
}
return caller.arguments[0];
}
Here the use of document.all to determine whether the Internet Explorer is not good practice, should use useragent to judge, jquery and other class libraries have a good implementation.
So the Test2 method above can be signed without modifying the method:
Copy Code code as follows:
function Test2 () {
var event = GetEvent (TEST2);
Alert (Geteventtarget (event). ID);
}
function Geteventtarget (event) {
if (document.all)
return event.srcelement;
return event.target;
}
Why can I write the GetEvent method and get the event?
Because the original event invocation in the Firefox event model was passed to the method, the GetEvent method can be written to get an event that evokes JavaScript.
screen.width-300) this.width=screen.width-300 "border=0>