Compatibility of Event Events

Source: Internet
Author: User

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?
  1. If (typeof (window. Event) = 'undefined ')
  2. Alert ('window. event is undefined .');
  3. Else
  4. 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;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.