简体   繁体   English

preventDefault()不起作用

[英]preventDefault() not working

//prevent default handler
function click_handler(event){            
  if(event.shiftKey){
      event.preventDefault();
  }
}
//event listener
var file_mgmt=document.querySelector('div.filemanager'); 
if(window.addEventListener){
        file_mgmt.addEventListener('click',click_handler,false);
    }else if(window.attachEvent){
        file_mgmt.attachEvent('onclick',click_handler);
    }else{
        file_mgmt.onclick=click_handler;
    }

basically file_mgmt is a div container which is enclosed with tag linking to another links. 基本上,file_mgmt是一个div容器,其中包含链接到另一个链接的标记。 Since Shift+click opens the link in new _blank tab i want to prevent it since it will damage the original page layout. 由于Shift +单击会在新的_blank选项卡中打开链接,因此我想阻止它,因为它会损坏原始页面布局。

Giving up after a while, i even tried following from Mozilla doc: Event.preventDefault() , the result being the same. 放弃一段时间后,我什至尝试从Mozilla doc跟踪以下事件: Event.preventDefault() ,结果是相同的。

Any Suggestions? 有什么建议么?

Preventing default action when clicking on a div will not do anything since by default it doesn't do anything anyway. 单击div时阻止默认操作将不会执行任何操作,因为默认情况下它不会执行任何操作。 You should attach the click handler to the actual link instead. 您应该将点击处理程序附加到实际链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM