控制+ s,返回 false 但仍调用另一个函数
$(document).keydown(function (e) {
if (e.keyCode == 83 && (navigator.platform.match("Mac") ? e.metaKey : e.ctrlKey)) {
function_which_does_a_lot_including_ajax_calls();
return false;
}
});
我正在尝试调用 control+S 上的方法,并且对 function_which_does_a_lot_include_ajax_calls()
的调用会阻止代码到达“return false”及时,表示浏览器默认提示“是否保存此网站”窗口。
如果我将其更改为简单的:
$(document).keydown(function (e) {
if (e.keyCode == 83 && (navigator.platform.match("Mac") ? e.metaKey : e.ctrlKey)) {
// function_which_does_a_lot_including_ajax_calls();
return false;
}
});
...将触发 return false,并且什么也不会发生(没有默认提示)。
有没有办法我可以做很多事情,但仍然返回 false,以便浏览器不执行其默认行为?谢谢!
$(document).keydown(function (e) {
if (e.keyCode == 83 && (navigator.platform.match("Mac") ? e.metaKey : e.ctrlKey)) {
function_which_does_a_lot_including_ajax_calls();
return false;
}
});
I'm trying to call a method on control+S, and, the call to function_which_does_a_lot_including_ajax_calls()
stops the code from reaching the "return false" in time, which means that the browser prompts the default "Do you want to save this website" window.
If I change it to simply:
$(document).keydown(function (e) {
if (e.keyCode == 83 && (navigator.platform.match("Mac") ? e.metaKey : e.ctrlKey)) {
// function_which_does_a_lot_including_ajax_calls();
return false;
}
});
... the return false is triggered, and nothing happens (no default prompt).
Is there a way I can do a lot of stuff, but still return false so that the browser does not do its default behavior? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
e.preventDefault()
阻止浏览器的默认行为(类似于return false
)。示例:
更新:替代方法。下面的代码维护范围、
this
和事件对象引用,同时首先处理return false
语句:Use
e.preventDefault()
to prevents browser's default behaviour (similar toreturn false
).Example:
Update: Alternative method. The code below maintains the scope,
this
and event object reference, while thereturn false
statement is processed first:我不确定我是否理解您所描述的行为,但您可以尝试以下操作:
I'm not sure I understand the behaviour you described but you can try something like: