jQuery:preventDefault()不能在输入/点击事件上工作? [英] jQuery: preventDefault() not working on input/click events?

查看:147
本文介绍了jQuery:preventDefault()不能在输入/点击事件上工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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(); });

事实上,我可以在FF中输入字段的每个元素EXCEPT知道为什么或可以指出我的一些文档?

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

编辑:

对不起,阅读评论后,我意识到我应该澄清一些事情。

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

1)上面的代码在某种意义上起作用。代码可以通过点击哪个按钮进行排序,它只是不在乎我说e.preventDefault(),并且右键单击菜单仍然弹出。换句话说,如果你对e.button发出警报,你会得到你的1或0左边和2为正确...但它只是笑我,仍然显示死亡的默认菜单!

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)如果我将jQuery选择器放在任何其他元素(除了输入)之外,那么一切都将工作,FF将会遵守preventDefault()调用,默认的右键菜单不会显示。 / p>

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.

推荐答案

对不起,我知道这是一个cop-out答案,因为我无法弄清楚为什么webkit浏览器不喜欢你搞乱输入字段事件,但无论如何,这是有用的。所以解决方案是将侦听器应用到整个窗口,然后询问目标是否具有要将效果应用于...并从那里走过的类。说实话,这可能是一个更好的解决方案,因为对于我的特定应用程序,这允许我将这个功能应用到我想要的任何元素。

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!");
        }
    });
}

这篇关于jQuery:preventDefault()不能在输入/点击事件上工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆