简体   繁体   English

禁用鼠标中键进行模态对话框

[英]Disable middle mouse button for modal dialog

I have such a problem: I have link which on click opens ajaxFormDialog in modal dialog on top of the current page. 我有这样一个问题:我有一个链接,单击该链接可在当前页面顶部的模式对话框中打开ajaxFormDialog。 But when I click middle button, it opens in new tab, and everything is not in modal window, but currently on new tab page and looks bad. 但是,当我单击中间按钮时,它将在新选项卡中打开,并且所有内容都不在模式窗口中,而是当前在新选项卡页面上,并且看起来很糟糕。 So, my question would be, how it is possible to disable middle mouse button click for current link? 因此,我的问题是,如何禁用当前链接的鼠标中键单击?

<a class="ajaxFormDialog" ...></a>
<script>
    $(function (){
       $('a.ajaxFormDialog').live("click", function(e) {
           $.ajax({
                type: "POST",
                url: $("#formContent form").attr("action"),
                data: $("#formContent form").serialize(),
                success: function(data) {
                            //... do something
                         }
                 });
       });
</script>

UPD I used your suggested UPD我用了你的建议

if(e.which == 2) {
   e.preventDefault();
}

it maybe preventsDefault, but still opens new tab with that form. 它可能会阻止Default,但仍会使用该表单打开新标签页。 When I click with middle/mousewheel button on link it doesn`t even show me, that he entered this $(function (){ $('a.ajaxFormDialog').on("click", function(e) { ... 当我用链接上的中间/鼠标按钮单击时,它甚至没有显示我,他输入了此$(function(){$('a.ajaxFormDialog')。on(“ click”,function(e){.. 。

UPD2 I wrote such code: UPD2我写了这样的代码:

$(function (){
   $('a.ajaxFormDialog').live("click", function(e) {
       console.log("Which button is clicked: " + e.which);
       if(e.which == 2) {
          e.preventDefault();
       }
       // rest of the code...

So when I click left mouse button, console shows me "Which button is clicked: 1", but when I click middle/mousewheel button it shows me nothing and still opens in new tab. 因此,当我单击鼠标左键时,控制台会向我显示“单击了哪个按钮:1”,但是当我单击中/鼠标轮按钮时,它什么也没有显示,并且仍在新选项卡中打开。

$("a.ajaxFormDialog").on('click', function(e) { 
   if( e.which == 2 ) {
      e.preventDefault();
   }
});

UPDATED : 更新时间

The default function of middle mouse button can't be disabled in firefox. 在firefox中不能禁用鼠标中键的默认功能。 As stated here . 如前所述这里

Firefox and the other Gecko browsers have released control of the right mouse button, but the default action of a middle click can not be disabled. Firefox和其他Gecko浏览器已释放了对鼠标右键的控制,但是无法禁用默认的中键单击操作。 You can change what the default action is by editing the middlemouse settings on the "about:config" URL, but Javascript can't cancel them. 您可以通过编辑“ about:config” URL上的中间鼠标设置来更改默认操作,但是Javascript无法取消它们。

You can find similar link of your post here . 您可以在此处找到您帖子的类似链接。

The very close working solution in some modern browser (like Chrome) is: 在某些现代浏览器(例如Chrome)中,非常接近的解决方案是:

if (event.preventDefault)
    event.preventDefault();
else
    event.returnValue= false;
return false;

Since Firefox (and, presumably, Opera as well) have taken middle-click behavior out of the developers' hands, I would suggest replacing the anchor node(s) with a different node, eg <span> . 由于Firefox(可能还包括Opera)已经将中点点击行为从开发人员手中夺走了,我建议用其他节点替换锚节点,例如<span>

This seems semantically OK, since the <a> tag no longer functions as an actual link in your usage scenario. 从语义<a>这还可以,因为<a>标记在您的使用场景中不再充当实际的链接。 It can maintain its appearance using CSS. 它可以使用CSS保持外观。

A live example can be found in this jsfiddle . 可以在此jsfiddle中找到一个实时示例。

For this kind of markup: 对于这种标记:

<a class="ajaxFormDialog" href="#">replaced link</a>

you can use CSS such as: 您可以使用CSS,例如:

a, .ajaxFormDialog {
    color: orange;
    text-decoration: underline;
    cursor: hand;
}

a:hover, .ajaxFormDialog:hover {
    background: orange;
    color: white;

}

and replace the anchor with a span , including the ability to store any desired property and maintain any child nodes (if any). 并用span替换锚,包括存储任何所需属性和维护任何子节点(如果有)的能力。 You can later retrieve those properties in the event handler. 您以后可以在事件处理程序中检索这些属性。

The example code is as follows: 示例代码如下:

var genericHandler = function(e) {
    var el = $(e.target);
    var href = el.data('href');
    //extract data
    console.log('clicked span: ',el, ' with stored href: ', href);
    //do stuff here
};

$('a.ajaxFormDialog').replaceWith(function(){
    var el = $(this);
    console.log('replacing element: ', el);
    return $("<span>").addClass('ajaxFormDialog').append($(this).contents()).
        data('href', el.attr('href')). //you can store other data as well
        click(genericHandler);
});

This seems to be the lesser of all evils, if you wish to avoid middle-click side effects, for the moment. 如果您希望暂时避免点击中毒的副作用,那么这似乎是种种弊病。

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

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