1

我想检测用户是否右键单击表中的一行(数据表供电)。

现在,如果我使用非 ajax 源,以下代码可以正常工作:

oTable.$('tr').mousedown(function(e) {
    if (e.which === 3) { //Right Mousebutton was clicked
        window.sData = oTable.fnGetData( this );
        jQuery(this).contextMenu({ x: e.pageX + 10, y: e.pageY + 10});
    }
});

但是,如果我使用 ajax 源,它就不起作用,所以我环顾四周并尝试了:

jQuery('#myTable tbody').on( 'click', 'tr', function (e) {
    alert("a click!");
    if (e.which === 3) { //Right Mousebutton was clicked
        alert("actually it was a right click!");
    }
});

此代码确实检测到常规点击,但如果无法识别右键点击。

我究竟做错了什么?

4

2 回答 2

3

Something like this?

jQuery('#myTable tbody').mousedown(function(e){ 
    if( e.button == 2 ) { 
      alert('Right mouse button!'); 
      return false; 
    } 
    return true; 
  }); 
于 2013-07-11T19:08:47.297 回答
3

Alexey 的代码在您第一次加载表格时工作,但是当您对其执行一些 ajax 操作时它停止工作。所以.on(...)必须使用该方法。

我目前使用的代码如下所示:

jQuery('#myTable tbody').on( 'mousedown', 'tr', function (e) {
     alert("mouse event detected!"); 
     if( e.button == 2 ) { 
        alert('Right mouse button!'); 
        return false; 
     } 
     return true; 
}); 
于 2013-07-11T19:51:14.547 回答