Use Bootstrap 3 dropdown menu as context menu

后端 未结 4 871
不思量自难忘°
不思量自难忘° 2020-11-29 16:12

Using Bootstrap 3, how can I place the dropdown menu at the cursor and open it from code?

I need to use it on a table as a context menu for its rows.

4条回答
  •  庸人自扰
    2020-11-29 17:07

    It is possible. I made you a working demo to give a good start.

    Working demo (Right click on any table row to see it in action)

    First create your dropdown menu, hide it and change its position to absolute:

    #contextMenu {
      position: absolute;
      display:none;
    }
    

    Then bind a contextmenu event to your table rows so it shows dropdown/context menu and position it at the cursor:

    var $contextMenu = $("#contextMenu");
    
    $("body").on("contextmenu", "table tr", function(e) {
       $contextMenu.css({
          display: "block",
          left: e.pageX,
          top: e.pageY
       });
       return false;
    });
    

    Then when user select an option hide dropdown/context menu:

    $contextMenu.on("click", "a", function() {
       $contextMenu.hide();
    });
    

提交回复
热议问题