简体   繁体   English

JS总是通过mousedown获得相同的事件对象“ e”参数

[英]JS Always getting the same event object “e” parameter with mousedown

I'm trying to handle a middle mouse button click event with JQuery on a DataTable ( https://datatables.net/ ). 我正在尝试使用DataTable( https://datatables.net/ )上的JQuery处理鼠标中键单击事​​件。 Here is my code. 这是我的代码。

var tbl = document.getElementById("entries");
$(tbl).on('mousedown', function (e) { 
     e.preventDefault();
     if (e.which == 2) {
         var table = window.table_entries;
         var data = table.dataTable.row($(e.detail)).data();
         window.open("/plugin/Changes/@Model.Revision/" + data.BuildId, '_blank');
     }
});

I'm always getting the same BuildId (284), no matter where I click. 无论我在哪里单击,我总是得到相同的BuildId(284)。 How can I get the correct row? 如何获得正确的行?

I also have another code snippet, which works perfectly fine 我还有另一个代码段,可以很好地工作

tbl.addEventListener("cdt.click", function (e) {
        var table = window.table_entries;
        var data = table.dataTable.row($(e.detail)).data();
        window.open("/plugin/Changes/@Model.Revision/" + data.BuildId, '_blank');
        window.location("/plugin/Changes/@Model.Revision/" + data.BuildId);
 });

Thanks in advance! 提前致谢!

if you want to check de middle button click with jquery check this code 如果要检查中间按钮,请单击jquery检查此代码

$("#foo").on('click', function(e) { 
  if( e.which == 2 ) {
  e.preventDefault();
  alert("middle button"); 
  }
});

From this question Triggering onclick event using middle click 从这个问题开始, 使用中间点击触发onclick事件

And if you want to check downmouse you can check this other @KyleMit answer Detect middle button click (scroll button) with jQuery 而且,如果您想检查鼠标,则可以检查其他@KyleMit答案使用jQuery检测中键单击(滚动按钮)

@Jordi Jordi (because I can't comment right now) : Based on JQuery's documentation, click & mousedown both works. @Jordi Jordi(因为我现在无法评论):根据JQuery的文档,单击和鼠标按下均可以。

$('h1').on('mousedown', function(e) {
    alert(e.which);
});

Will display 1 for LClick, 2 for Middle, 3 for RClick. 将为LClick显示1,为中显示2,为RClick显示3。 But this isn't the question. 但这不是问题。 If you're getting the same BuildId, it's because your selector isn't the good one. 如果您获得相同的BuildId,那是因为选择器不是很好的选择器。

If you're searching to get an exact row, you should change your selector like this : 如果要搜索准确的行,则应像这样更改选择器:

$('td').on('mousedown', function (e) { 
    e.preventDefault();
    if (e.which == 2) {
        // Here you should get the row like this :
        var row = $(this).parent();
    }
});

Now it's your job to do what you want with this. 现在,您要做的就是执行此操作。 The var "row" will contain the TR, meaning the row you just give a click. var“行”将包含TR,表示您只需单击一下行。

EDIT : Note that your second code snippet doesn't include e.preventDefault(). 编辑:请注意,您的第二个代码段不包含e.preventDefault()。 Maybe it's the reason this second one works ? 也许这是第二个作品起作用的原因吗?

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

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