简体   繁体   English

为什么返回带警报的false而不是console.log

[英]Why does return false work with alert but not console.log

I have a problem. 我有个问题。 With this html: 与此HTML:

<div id="d"><div>Click me!</div><div>Or me</div></div>

When I use this code: 当我使用此代码时:

$("#d").on("mousedown", "div", function (e){
    console.log('clicked');
    if (e.which == 3) alert('left button clicked');
    return false;
});

It prevents the context menu from showing and alerts, but when I use this code: 它阻止了上下文菜单的显示和警报,但是当我使用此代码时:

$("#d").on("mousedown", "div", function (e){
    console.log('clicked');
    if (e.which == 3) console.log('left button clicked');
    return false;
});

It logs to the console, but doesn't prevent the context menu. 它登录到控制台,但不阻止上下文菜单。 I'm confused. 我糊涂了。

Here's a JSFiddle demonstrating the problem. 这是JSFiddle演示的问题。

Oh, and please be patient if you happen to find a duplicate or something. 哦,如果您碰巧发现重复的东西,请耐心等待。 I did try to find it. 我确实尝试找到它。

You're not preventing the context menu with the first code. 您并没有使用第一个代码阻止上下文菜单。 SOme browsers just don't open it anymore because of the alert window getting the focus. SOme浏览器只是不再打开它,因为alert窗口已成为焦点。 It's not the return false that prevents the menu, it's the alert itself. 不是阻止菜单return false ,而是alert本身。

I added an updated fiddle: http://jsfiddle.net/2D32L/12/ 我添加了一个更新的小提琴: http : //jsfiddle.net/2D32L/12/

$("#f").on("contextmenu", "div", function (e) {
    e.preventDefault();
});

See this question for more information. 有关更多信息,请参见此问题

For the rest I agree with Johannes H. ; 对于其余部分,我同意Johannes H.的观点 it's not the return value that cancels the contextmenu, it's the alert that does. 取消上下文菜单的不是返回值,而是警报。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM