问答详情
源自:3-2 IE中的事件对象

I have a question,是关于preventDefault的

<a href="https://www.baidu.com" id="goto">跳转</a>
<script>
    var event={
        // 增添事件
        addEvent:function(ele,type,handle){
            if(ele.addEventListener){
                ele.addEventListener(type,handle,false);
            }else if(ele.addElement){
                ele.addElement("on"+type,handle);
            }else{
                ele["on"+type] = handle;
            }
        },
        //阻止默认行为
        preventDefault:function(event){
            if(event.preventDefault){
                event.preventDefault();
            }else{
                event.returnValue=false;
             }
         }
     };
         var goto=document.getElementById("goto");
         event.addEvent(goto,"click",function(e){
             e=event || window.event;
             event.preventDefault(e);
         });
</script>

如上,可是在浏览器中无论chrome还是IE都无效啊,该跳转还是跳转了.

提问者:专注程序不可自拔 2018-11-07 10:30

个回答

  • MADAO果实能力
    2018-11-16 16:55:07

    应该是因为定义对象名为event的原因的,我复制你的代码改成eventHandler没问题