我知道考虑到可访问性,这不是最好的做法,但我确实需要禁止用户在 IE7 中使用 CTRL+ 放大页面。
我通过以下方式让它适用于其他浏览器,但 IE7 似乎忽略了“return false”:
$(window).keydown(function (e) {
alert('key is down'); // this fires
return false; // but this has no effect in IE7!
});
我知道考虑到可访问性,这不是最好的做法,但我确实需要禁止用户在 IE7 中使用 CTRL+ 放大页面。
我通过以下方式让它适用于其他浏览器,但 IE7 似乎忽略了“return false”:
$(window).keydown(function (e) {
alert('key is down'); // this fires
return false; // but this has no effect in IE7!
});
This is better and correct way:
$(document).ready(function() {
var ctrl = false;
$(document).keydown(function(e){
// disable ctrl + +/-
if(ctrl && (e.keyCode == 107 || e.keyCode == 109)) {
alert('Zoom is disabled!');
return false;
}
if(e.keyCode == 17) {
ctrl = true;
// disable ctrl + scroll
$(document).bind('scroll', function() {
if(ctrl) {
alert('Zoom is disabled!');
return false;
}
});
}
})
$(document).keyup(function(e) {
if(e.keyCode == 17) {
ctrl = false;
$(document).unbind('scroll');
}
});
});
如果最终用户的浏览器在访问您的页面之前已经设置了缩放,那么这是没有意义的。
尝试将 keydown 附加到文档:
$(document).keydown(function (e) {
alert('key is down');
return false;
});
我没有 IE7 可以在 ATM 上测试,但这应该可以
$(window).keydown(function (e) {
alert('key is down'); // this fires
e.preventDefault(); // This is a standard jQuery way of
// preventing the default action
return false; // Therefore you shouldn't need this.
});
简单的答案。对于 IE,你需要Event.stop(e);而不是return false;