preventDefault not working for keydown in Safari 5.0.4
I have the following code that binds Alt+b to do something:
(function(jQuery){
$(document).ready(function() {
$(document).bind('keydown', 'alt+b', function(event) {
// do stuff
event.preventDefault();
});
});
})( jQuery );
When using Safari browser (v5.0.4) on PC this triggers the menu to show (Bookmarks). Is there a way to prevent this behavior? preventDefault works with Chrome and Firefox in this case. I also tried to 'return f开发者_JAVA百科alse' but it doesn't work either.
Note: it seems that my code works until I've made the menu visible for the first time. After that Alt+b triggers the bookmarks to show even if the menu is hidden.
I don't have your version of Safari (I couldn't test) but you can give this a try http://jsfiddle.net/LnvGR/2/
(function(jQuery){
var keys = {};
$(document).keydown(function (e) {
keys[e.which] = true;
var kValues = '';
for (var i in keys) {
if (!keys.hasOwnProperty(i)) continue;
kValues += i;
}
if (kValues == "1866" || kValues == "6618")
{
e.preventDefault();
return false;
}
});
$(document).keyup(function (e) {
delete keys[e.which];
});
})( jQuery );
Reading all keys, then checking for the combination and calling preventDefault();
Try this:
(function(jQuery){
$(document).ready(function() {
$(document).bind('keydown', function(e) {
if(e.keyCode==66){
// do stuff
e.preventDefault();
}
});
});
})( jQuery );
Tested in safari, and it doesn't trigger bookmarks.
精彩评论