Window. Event Problems
Problem description: window. event can only be run in IE, but not in Firefox, because Firefox Event can only be used in the event.
Solution: add the event parameter to the function where the event occurs and use VaR myevent = EVT? EVT :( window. event? Window. Event: NULL)
After being called on the Firefox page, the windows. event object is undefined:
View plaincopy to clipboardprint?
- If (typeof (window. Event) = 'undefined ')
- Alert ('window. event is undefined .');
- Else
- Alert ('window. event is defined .');
The above code is executed in Firefox and the result is: "window. event is undefined .". IE and chrome: "window. event is defined ."
1. Pass the event parameter in the function
Pass the event parameter in the function, so that we can get the event compatible with IE and FF, as shown in the following function:
function _test(evt)
{VaR src = EVT. srcelement | evt.tar get; // gets the source object of the trigger event
Alert (SRC. Value); // print the Value Attribute of the object
}
In this case, we should:
<Input type = 'button 'value = 'click me' onclick = '_ test (event)'/>
2. Do not pass event objects in function calls.
Although no parameter is passed in the function, this has no effect in IE, because window. event is a global object and can be called directly anywhere, but it won't work in ff. So here we need to use another method to obtain it, as shown below:
unction _test2()
{VaR EVT = Window. Event | arguments. callee. Caller. Arguments [0]; // obtain the event object
VaR src = EVT. srcelement | evt.tar get; // gets the source object of the trigger event
VaR ikeycode = EVT. keycode | EVT. Which; // get the button code
Alert (SRC. Value); // print the Value Attribute of the object
if (window.navigator.userAgent.indexOf("IE")>=1){ evt.keyCode =0;
evt.returnValue=false;
}else{ evt.preventDefault();
}
}
When using this function, you can directly enter the function name, as shown in the following figure:
<Input type = 'button 'value = 'click me2' onclick = '_ Test2 ()'/>
Event. srcelement
Problem description: in IE, the even object has the srcelement attribute, but does not have the target attribute. In Firefox, the even object has the target attribute, but does not have the srcelement attribute.
Solution: Use srcobj = event. srcelement? Event. srcelement: event.tar get;