开发者

event.returnValue in IE6

The following code should not open a new window in IE and Firefox. Its not opening in Firefox, but it is opening in IE. Whats going wrong?

var EventLib = {
    "preventDefault"  : function(event){
        if(event.preventDefault) {
            event.preventDefault();
        }else{
            window.event.returnValue = false;
        }           
    }
}

window.onload = function(){
  var elem = document.getElementById("link");
    elem.onclick = function(e){
    EventLib.preventDefault(e);
    }
}

and the HTML is:

<a id="link" href="开发者_如何学Pythonhttp://www.google.com" target="_blank">Click</a>


It could be that evaluating the expression event.preventDefault throws an error when event is undefined. Try using if (event && event.preventDefault) rather than just if (event.preventDefault).


Just change function as I have shown it below, it will work

var EventLib = {
        "preventDefault"  : function(event){
            if(!event)
                event = window.event;
            if(event.preventDefault) {
                event.preventDefault();
            }else{
                window.event.returnValue = false;
            }           
        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜