e.preventDefault(); behaviour not working in Firefox?

本秂侑毒 提交于 2019-12-04 05:40:22

问题


I have this basic function for handling the key event, everything works great. However, in Firefox 9.0.1 it seems I can't prevent the default event which is showing of bookmarks.

Is there any solution to prevent the default behaviour in FF?

$(document).keydown(function(evt) {     
    if (evt.which == 66 && evt.ctrlKey) {                             
         if (evt.preventDefault) {
             evt.preventDefault();
         } else {
             evt.returnValue = false;
         }    
         alert("Ctrl+B pressed");
         return false;                      
    }
});

回答1:


Seems like some sort of bug regarding alert. Try this:

$(document).keydown(function(evt) {     
    if (evt.which == 66 && evt.ctrlKey) {                             
         if (evt.preventDefault) {
             evt.preventDefault();
         } else {
             evt.returnValue = false;
         }    
         console.log("Ctrl+B pressed");
         return false;                      
    }
});

Doesn't open the Bookmarks Toolbar for me now. I assume you don't actually want to alert do you? I think you can just call your method as long as it doesn't contain an alert.



来源:https://stackoverflow.com/questions/8757301/e-preventdefault-behaviour-not-working-in-firefox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!