jQuery:preventDefault() 不适用于输入/单击事件?

发布于 2024-08-31 06:34:58 字数 1376 浏览 9 评论 0原文

我想在用户右键单击输入字段时禁用默认上下文菜单,以便我可以显示自定义上下文菜单。一般来说,通过执行以下操作来禁用右键菜单非常容易:

$([whatever]).bind("click", function(e) { e.preventDefault(); });

事实上,我可以对除了 FF 中的输入字段之外的几乎每个元素执行此操作 - 有人知道为什么或者可以向我指出一些文档吗?

这是我正在使用的相关代码,谢谢大家。

HTML:

<script type="text/javascript">
 var r = new RightClickTool();
</script>

<div id="main">
  <input type="text" class="listen rightClick" value="0" />  
</div>

JS:

function RightClickTool(){

var _this = this;
var _items = ".rightClick";

$(document).ready(function() { _this.init(); });

this.init = function() {
 _this.setListeners(); 
}

this.setListeners = function() {
 $(_items).click(function(e) {
  var webKit = !$.browser.msie && e.button == 0;
  var ie = $.browser.msie && e.button == 1;

  if(webKit||ie)
  { 

   // Left mouse...do something()

  } else if(e.button == 2) {
   e.preventDefault(); 

   // Right mouse...do something else();
  }

 });
}

} // Ends Class

编辑:

抱歉,在阅读评论后,我意识到我应该澄清一些事情。

1)上面的代码在某种意义上确实有效。代码能够对单击的按钮进行排序,它只是不在乎我说 e.preventDefault() 并且右键单击菜单仍然弹出。换句话说,如果你在 e.button 上设置警报,你会得到 1 或 0 代表左边,2 代表右边……但它只是嘲笑我,并且仍然显示该死的默认菜单!

2)如果我将 jQuery 选择器放在任何其他元素(输入除外)上,那么一切都会正常工作,FF 将尊重 PreventDefault() 调用,并且默认的右键单击菜单将不会显示。

I want to disable the default contextMenu when a user right-clicks on an input field so that I can show a custom contextMenu. Generally speaking, its pretty easy to disable the right-click menu by doing something like:

$([whatever]).bind("click", function(e) { e.preventDefault(); });

And in fact, I can do this on just about every element EXCEPT for input fields in FF - anyone know why or could point me towards some documentation?

Here is the relevant code I am working with, thanks guys.

HTML:

<script type="text/javascript">
 var r = new RightClickTool();
</script>

<div id="main">
  <input type="text" class="listen rightClick" value="0" />  
</div>

JS:

function RightClickTool(){

var _this = this;
var _items = ".rightClick";

$(document).ready(function() { _this.init(); });

this.init = function() {
 _this.setListeners(); 
}

this.setListeners = function() {
 $(_items).click(function(e) {
  var webKit = !$.browser.msie && e.button == 0;
  var ie = $.browser.msie && e.button == 1;

  if(webKit||ie)
  { 

   // Left mouse...do something()

  } else if(e.button == 2) {
   e.preventDefault(); 

   // Right mouse...do something else();
  }

 });
}

} // Ends Class

EDIT:

Sorry, after reading the comments I realize that I should clarify a few things.

1) The code above does work...in a sense. The code is able to sort through which button was clicked, it just doesn't care that I say e.preventDefault() and the right-click menu still pops up. In other words, if you put an alert on e.button you would get your 1 or 0 for left and 2 for right...but it just laughs at me and still shows the damned default menu!

2) If I put the jQuery selector on ANY other element (other than input) then everything will work, FF will respect the preventDefault() call and the default right-click menu will not show.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

ゞ记忆︶ㄣ 2024-09-07 06:34:58

禁用默认上下文菜单的跨浏览器解决方案:

window.oncontextmenu = function() { return false };

要仅在给定元素内捕获 contextmenu 事件,请使用:

document.getElementById('myElement').oncontextmenu=function(){

    // Code to handle event
    return false;
}

如果您尝试使用 oncontextmenu 以外的方法来中断右键单击,则根据确切的事件场景和浏览器,您将获得不同的结果事件。

在 jQuery 中:

$('myElement').bind('contextmenu', function(){

    // Handle right-click event.
    return false;
});

您还可以使用 jQuery 的 event.which 来确定按下了哪个按钮,但您仍然需要取消默认的 contextmenu 事件:

   // Cancel default oncontextmenu event.
    $(element).bind('contextmenu', function(){ return false });

    $(element).mousedown(function(event){

        switch (event.which)

            case 1:
            // Left mouse
            break;

            case 2:
            // Middle mouse
            break;

            case 3:
            // Right mouse.
            break;

    });

我已经在 IE6(在 Wine 下)、Chrome 11、Firefox 3.6、Opera 11 中测试了这一点和野生动物园。

A cross-browser solution to disable the default context menu:

window.oncontextmenu = function() { return false };

To capture the contextmenu event only inside a given element use:

document.getElementById('myElement').oncontextmenu=function(){

    // Code to handle event
    return false;
}

You will get different results depending on the exact event scenario and browser if you try methods other than oncontextmenu for interrupting a right-click event.

In jQuery:

$('myElement').bind('contextmenu', function(){

    // Handle right-click event.
    return false;
});

You can also use jQuery's event.which to determine which button was pressed, but you will still have to cancel the default contextmenu event:

   // Cancel default oncontextmenu event.
    $(element).bind('contextmenu', function(){ return false });

    $(element).mousedown(function(event){

        switch (event.which)

            case 1:
            // Left mouse
            break;

            case 2:
            // Middle mouse
            break;

            case 3:
            // Right mouse.
            break;

    });

I've tested this in IE6 (under Wine), Chrome 11, Firefox 3.6, Opera 11 and Safari.

森林散布 2024-09-07 06:34:58

抱歉,大家,我知道这是一个逃避的“答案”,因为我一直无法弄清楚为什么 webkit 浏览器不喜欢你搞乱输入字段事件,但无论如何,这有效。因此,解决方案是将侦听器应用于整个窗口,然后询问目标是否具有您想要应用效果的类......然后从那里开始。老实说,这可能是一个更好的解决方案,因为对于我的特定应用程序,这允许我将此功能应用于我想要的任何元素。

var _class = "input.hideRightClick";

this.setListeners = function() {
    $(window).click(function(e) {
        if($(e.target).is(_class))
        {
            e.preventDefault(); 
            alert("hide!");
        }
    });
}

Sorry guys, I know this is a cop-out "answer" because I was never able to figure out exactly why webkit browsers don't like you messing with input field events but whatever, this works. So the solution is to apply the listener to the whole window and then just ask if the target has the class you want to apply the effect to...and go from there. To be honest, this is probably a better solution anyway because for my particular application, this allows me to apply this functionality to any element I want.

var _class = "input.hideRightClick";

this.setListeners = function() {
    $(window).click(function(e) {
        if($(e.target).is(_class))
        {
            e.preventDefault(); 
            alert("hide!");
        }
    });
}
滴情不沾 2024-09-07 06:34:58

我刚刚在 Firefox 3.6 中进行了测试,它正在执行第一个 if 语句,因此永远不会调用 e.preventDefault() 。

  var webKit = !$.browser.msie && e.button == 0; // TRUE IN FIREFOX 3.6

  var ie = $.browser.msie && e.button == 1;

  if(webKit||ie)
  { 

   // Left mouse...do something()

  } else if(e.button == 2) {
   e.preventDefault(); 

   // Right mouse...do something else();
  }

I just tested in Firefox 3.6 and it's proceeding into the first if statement, so the e.preventDefault() is never called.

  var webKit = !$.browser.msie && e.button == 0; // TRUE IN FIREFOX 3.6

  var ie = $.browser.msie && e.button == 1;

  if(webKit||ie)
  { 

   // Left mouse...do something()

  } else if(e.button == 2) {
   e.preventDefault(); 

   // Right mouse...do something else();
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文