【问题标题】:How to use jQuery ContextMenu to have 2 different menus one above the other inside a table?如何使用 jQuery ContextMenu 在一个表内有两个不同的菜单一个在另一个之上?
【发布时间】:2017-11-22 23:22:34
【问题描述】:

使用jQuery ContextMenu 插件或纯Javascript,是否可以使用2 个不同的上下文菜单(第一个在父元素上,第二个在子元素上)?

在我的 sn-p 中,我想打开 第一个菜单仅在右键单击时(在表格行上)并打开 第二个菜单仅在左键单击时按钮(在我的行内)。

我只为按钮设置了trigger: 'left',但是当我左键单击它时,两个菜单都显示为您在此处看到的:

$(function() {
    $.contextMenu({
        selector: '.context-menu-one', 
        callback: function(key, options) {
            var m = "clicked: " + key;
            window.console && console.log(m) || alert(m); 
        },
        items: {
            "edit": {name: "Edit", icon: "edit"},
            "cut": {name: "Cut", icon: "cut"},
           copy: {name: "Copy", icon: "copy"},
            "paste": {name: "Paste", icon: "paste"},
            "delete": {name: "Delete", icon: "delete"},
            "sep1": "---------",
            "quit": {name: "Quit", icon: function(){
                return 'context-menu-icon context-menu-icon-quit';
            }}
        }
    });
    
    $.contextMenu({
        selector: '.context-menu-two', 
              trigger: 'left',
        items: {
            "new": {name: "New", icon: "new"},
            "open": {name: "Open", icon: "open"}
        }
    });  
});
table{width:300px;height:100px}
tr {background:#222;color:#fff}
<link href="https://www.jqueryscript.net/css/jquerysctipttop.css" rel="stylesheet"/>
<link href="https://www.jqueryscript.net/demo/Feature-rich-Custom-jQuery-Context-Menu-Plugin-contextMenu/dist/jquery.contextMenu.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://www.jqueryscript.net/demo/Feature-rich-Custom-jQuery-Context-Menu-Plugin-contextMenu/dist/jquery.ui.position.js"></script>
<script src="https://www.jqueryscript.net/demo/Feature-rich-Custom-jQuery-Context-Menu-Plugin-contextMenu/dist/jquery.contextMenu.js"></script>
<table>
    <tbody>
        <tr role="row" class="context-menu-one">
            <td>
                <button class="context-menu-two">Left click</button>
            </td>
        </tr>
    </tbody>
</table>

有没有办法阻止我点击按钮时显示第一个菜单?

第一次更新

根据 Aswin Kumar 的回答,这两个菜单分别正确显示,但正如您从下面的 gif 中看到的那样,如果您尝试这样做:

  1. 点击按钮
  2. 将鼠标悬停在菜单上
  3. 将鼠标移到菜单外
  4. 单击鼠标左键关闭菜单

在这种情况下,用户无法关闭菜单(第 4 点)。是否有解决方法可以在菜单外单击鼠标左键关闭菜单?

【问题讨论】:

  • 据我所知,你可以在一个页面上有多个菜单,但在另一个页面中打开另一个上下文菜单,left-click 也不这么认为,你最多可以在菜单中有 3 个子菜单水平或更高,但不是那样。是否要在打开的上下文菜单下打开子菜单?
  • @MuhammadOmerAslam 感谢您提供的信息,是的,我相信我应该找到不同的东西...不,我不需要子菜单,但再次感谢
  • 但我仍然不明白你为什么要这样做,为什么要通过左键单击另一个上下文菜单上的菜单项来打开上下文。
  • @MuhammadOmerAslam 因为我需要用户能够右键单击整个表格行以打开菜单,同时能够左键单击按钮(行内)以打开不同的菜单。

标签: javascript jquery contextmenu


【解决方案1】:

使用 contextMenu 的 show 事件(可取消)和 jquery 的 hasClass 来验证目标元素。在z-index的帮助下

更新

4.点击鼠标左键关闭菜单(fixed)

$(function() {

  $(document).on('mousedown', function(e) {
      $('.context-menu-one').contextMenu('hide');
      $('.context-menu-two').contextMenu('hide');
      e.preventDefault();
      e.stopImmediatePropagation();
  });

  $.contextMenu({
    selector: '.context-menu-one',
    callback: function(key, options) {
      var m = "clicked: " + key;
      window.console && console.log(m) || alert(m);
    },
    events: {
      show: function() {
        if ($(event.target).hasClass('context-menu-two')) {
          return false
        }
        $('.context-menu-two').first().contextMenu('hide');
      }
    },
    autoHide: true,
    items: {
      "edit": {
        name: "Edit",
        icon: "edit"
      },
      "cut": {
        name: "Cut",
        icon: "cut"
      },
      copy: {
        name: "Copy",
        icon: "copy"
      },
      "paste": {
        name: "Paste",
        icon: "paste"
      },
      "delete": {
        name: "Delete",
        icon: "delete"
      },
      "sep1": "---------",
      "quit": {
        name: "Quit",
        icon: function() {
          return 'context-menu-icon context-menu-icon-quit';
        }
      }
    }
  });

  $.contextMenu({
    selector: '.context-menu-two',
    trigger: 'left',
    autoHide: true,
    items: {
      "new": {
        name: "New",
        icon: "new"
      },
      "open": {
        name: "Open",
        icon: "open"
      }
    }
  });
});
table {
  width: 300px;
  height: 100px
}

tr {
  background: #222;
  color: #fff
}

.context-menu-two {
  position: relative;
  z-index: 2;
}

.context-menu-root {
  z-index: 3!important;
}
<link href="https://www.jqueryscript.net/css/jquerysctipttop.css" rel="stylesheet" />
<link href="https://www.jqueryscript.net/demo/Feature-rich-Custom-jQuery-Context-Menu-Plugin-contextMenu/dist/jquery.contextMenu.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://www.jqueryscript.net/demo/Feature-rich-Custom-jQuery-Context-Menu-Plugin-contextMenu/dist/jquery.ui.position.js"></script>
<script src="https://www.jqueryscript.net/demo/Feature-rich-Custom-jQuery-Context-Menu-Plugin-contextMenu/dist/jquery.contextMenu.js"></script>
<table>
  <tbody>
    <tr role="row" class="context-menu-one">
      <td>
        <button class="context-menu-two">Left click</button>
      </td>
    </tr>
  </tbody>
</table>

【讨论】:

  • 谢谢。但试试这个: 1. 单击按钮 2. 鼠标悬停在菜单上 3. 将鼠标移到菜单外。在这种情况下,用户无法关闭它。是否有解决方法可以在菜单外单击鼠标左键关闭菜单?
  • 它不工作。 1. 左键单击按钮 2. 鼠标悬停菜单 3. 将鼠标移到菜单外 4. 单击鼠标左键将其关闭。第 4 点不起作用。
  • @SimonOrro 行为正确,hideonsecondtrigger 功能在此版本中不可用。既然你问它更新了解决方法
  • 对我来说 ContextMenu 插件的版本无关紧要,我感兴趣的是它可以正常工作。请使用 gif 检查更新后的问题。
  • @SimonOrro 现在检查。我认为主要问题已经过去了。我说的对吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-14
  • 1970-01-01
  • 1970-01-01
  • 2018-05-08
  • 1970-01-01
相关资源
最近更新 更多