event.preventDefault() function not working in IE

后端 未结 11 2561
栀梦
栀梦 2020-11-22 02:22

Following is my JavaScript (mootools) code:

$(\'orderNowForm\').addEvent(\'submit\', function (event) {
    event.prev         


        
相关标签:
11条回答
  • 2020-11-22 02:38

    I know this is quite an old post but I just spent some time trying to make this work in IE8.

    It appears that there are some differences in IE8 versions because solutions posted here and in other threads didn't work for me.

    Let's say that we have this code:

    $('a').on('click', function(event) {
        event.preventDefault ? event.preventDefault() : event.returnValue = false;
    });
    

    In my IE8 preventDefault() method exists because of jQuery, but is not working (probably because of the point below), so this will fail.

    Even if I set returnValue property directly to false:

    $('a').on('click', function(event) {
        event.returnValue = false;
        event.preventDefault();
    });
    

    This also won't work, because I just set some property of jQuery custom event object.

    Only solution that works for me is to set property returnValue of global variable event like this:

    $('a').on('click', function(event) {
        if (window.event) {
            window.event.returnValue = false;
        }
        event.preventDefault();
    });
    

    Just to make it easier for someone who will try to convince IE8 to work. I hope that IE8 will die horribly in painful death soon.

    UPDATE:

    As sv_in points out, you could use event.originalEvent to get original event object and set returnValue property in the original one. But I haven't tested it in my IE8 yet.

    0 讨论(0)
  • 2020-11-22 02:39

    I was helped by a method with a function check. This method works in IE8

    if(typeof e.preventDefault == 'function'){
      e.preventDefault();
    } else {
      e.returnValue = false;
    }
    
    0 讨论(0)
  • 2020-11-22 02:41

    Mootools redefines preventDefault in Event objects. So your code should work fine on every browser. If it doesn't, then there's a problem with ie8 support in mootools.

    Did you test your code on ie6 and/or ie7?

    The doc says

    Every event added with addEvent gets the mootools method automatically, without the need to manually instance it.

    but in case it doesn't, you might want to try

    new Event(event).preventDefault();
    
    0 讨论(0)
  • 2020-11-22 02:46
    if (e.preventDefault) {
        e.preventDefault();
    } else {
        e.returnValue = false;
    }
    

    Tested on IE 9 and Chrome.

    0 讨论(0)
  • 2020-11-22 02:48

    If you bind the event through mootools' addEvent function your event handler will get a fixed (augmented) event passed as the parameter. It will always contain the preventDefault() method.

    Try out this fiddle to see the difference in event binding. http://jsfiddle.net/pFqrY/8/

    // preventDefault always works
    $("mootoolsbutton").addEvent('click', function(event) {
     alert(typeof(event.preventDefault));
    });
    
    // preventDefault missing in IE
    <button
      id="htmlbutton"
      onclick="alert(typeof(event.preventDefault));">
      button</button>
    

    For all jQuery users out there you can fix an event when needed. Say that you used HTML onclick=".." and get a IE specific event that lacks preventDefault(), just use this code to get it.

    e = $.event.fix(e);
    

    After that e.preventDefault(); works fine.

    0 讨论(0)
  • 2020-11-22 02:51

    FWIW, in case anyone revisits this question later, you might also check what you are handing to your onKeyPress handler function.

    I ran into this error when I mistakenly passed onKeyPress(this) instead of onKeyPress(event).

    Just something else to check.

    0 讨论(0)
提交回复
热议问题