控制+ s,返回 false 但仍调用另一个函数

发布于 2024-12-14 06:00:50 字数 782 浏览 2 评论 0原文

$(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鹤仙姿 2024-12-21 06:00:50

使用 e.preventDefault() 阻止浏览器的默认行为(类似于 return false)。

示例:

$(document).keydown(function (e) {
    if (condition_says_NO_to_default) {
        e.preventDefault();
        .... //Rest of code

更新:替代方法。下面的代码维护范围、this 和事件对象引用,同时首先处理 return false 语句:

$(document).keydown(function (e) {
    var $this = this; //Save reference for later
    setTimeout(function(){
        (function(event){
            //Your code here
        }).call($this, e);
    }, 1); // An extremely low timeout.
    return false;
});

Use e.preventDefault() to prevents browser's default behaviour (similar to return false).

Example:

$(document).keydown(function (e) {
    if (condition_says_NO_to_default) {
        e.preventDefault();
        .... //Rest of code

Update: Alternative method. The code below maintains the scope, this and event object reference, while the return false statement is processed first:

$(document).keydown(function (e) {
    var $this = this; //Save reference for later
    setTimeout(function(){
        (function(event){
            //Your code here
        }).call($this, e);
    }, 1); // An extremely low timeout.
    return false;
});
初懵 2024-12-21 06:00:50

我不确定我是否理解您所描述的行为,但您可以尝试以下操作:

$(document).keydown(function (e) {
    if (e.keyCode == 83 && (navigator.platform.match("Mac") ? e.metaKey : e.ctrlKey)) {
        setTimeout(function_which_does_a_lot_including_ajax_calls, 100);
        return false;
    }
});

I'm not sure I understand the behaviour you described but you can try something like:

$(document).keydown(function (e) {
    if (e.keyCode == 83 && (navigator.platform.match("Mac") ? e.metaKey : e.ctrlKey)) {
        setTimeout(function_which_does_a_lot_including_ajax_calls, 100);
        return false;
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文