event.preventDefault() function not working in IE

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

Following is my JavaScript (mootools) code:

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


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

    return false in your listener should work in all browsers.

    $('orderNowForm').addEvent('submit', function () {
        // your code
        return false;
    }
    
    0 讨论(0)
  • 2020-11-22 02:57

    Here's a function I've been testing with jquery 1.3.2 and 09-18-2009's nightly build. Let me know your results with it. Everything executes fine on this end in Safari, FF, Opera on OSX. It is exclusively for fixing a problematic IE8 bug, and may have unintended results:

    function ie8SafePreventEvent(e) {
        if (e.preventDefault) {
            e.preventDefault()
        } else {
            e.stop()
        };
    
        e.returnValue = false;
        e.stopPropagation();
    }
    

    Usage:

    $('a').click(function (e) {
        // Execute code here
        ie8SafePreventEvent(e);
        return false;
    })
    
    0 讨论(0)
  • 2020-11-22 02:59

    preventDefault is a widespread standard; using an adhoc every time you want to be compliant with old IE versions is cumbersome, better to use a polyfill:

    if (typeof Event.prototype.preventDefault === 'undefined') {
        Event.prototype.preventDefault = function (e, callback) {
            this.returnValue = false;
        };
    }
    

    This will modify the prototype of the Event and add this function, a great feature of javascript/DOM in general. Now you can use e.preventDefault with no problem.

    0 讨论(0)
  • 2020-11-22 03:00

    To disable a keyboard key after IE9, use : e.preventDefault();

    To disable a regular keyboard key under IE7/8, use : e.returnValue = false; or return false;

    If you try to disable a keyboard shortcut (with Ctrl, like Ctrl+F) you need to add those lines :

    try {
        e.keyCode = 0;
    }catch (e) {}
    

    Here is a full example for IE7/8 only :

    document.attachEvent("onkeydown", function () {
        var e = window.event;
    
        //Ctrl+F or F3
        if (e.keyCode === 114 || (e.ctrlKey && e.keyCode === 70)) {
            //Prevent for Ctrl+...
            try {
                e.keyCode = 0;
            }catch (e) {}
    
            //prevent default (could also use e.returnValue = false;)
            return false;
        }
    });
    

    Reference : How to disable keyboard shortcuts in IE7 / IE8

    0 讨论(0)
  • 2020-11-22 03:01

    in IE, you can use

    event.returnValue = false;
    

    to achieve the same result.

    And in order not to get an error, you can test for the existence of preventDefault:

    if(event.preventDefault) event.preventDefault();
    

    You can combine the two with:

    event.preventDefault ? event.preventDefault() : (event.returnValue = false);
    
    0 讨论(0)
提交回复
热议问题