添加标准的右键单击选项以自定义右键单击

时间:2015-04-22 13:06:07

标签: javascript jquery contextmenu right-click

如果可能的话,我想添加标准选项,例如检查元素,视图源等,以自定义右键单击。

这是我的JavaScript ......

<script type="text/javascript">
  $(document).bind("contextmenu", function (event) {

    // Avoid the real one
    event.preventDefault();

    // Show contextmenu
    $(".custom-menu").finish().toggle(100).

    // In the right position (the mouse)
    css({
        top: event.pageY + "px",
        left: event.pageX + "px"
    });
});


// If the document is clicked somewhere
$(document).bind("mousedown", function (e) {

    // If the clicked element is not the menu
    if (!$(e.target).parents(".custom-menu").length > 0) {

        // Hide it
        $(".custom-menu").hide(100);
    }
});


// If the menu element is clicked
$(".custom-menu li").click(function(){

    // This is the triggered action name
    switch($(this).attr("data-action")) {

        // A case for each action. Your actions here
        case "first": alert("first"); break;
        case "second": alert("second"); break;
        case "third": alert("third"); break;
        case "fourth": alert("fourth"); break;
        case "fifth": alert("fifth"); break;
        case "sixth": alert("sixth"); break;
        case "seventh": alert("seventh"); break;
    }

    // Hide it AFTER the action was triggered
    $(".custom-menu").hide(100);
  });
</script>

这是我的CSS ......

.custom-menu {
    display: none;
    z-index: 1000;
    position: absolute;
    overflow: hidden;
    border: 1px solid #CCC;
    white-space: nowrap;
    font-family: sans-serif;
    background: #FFF;
    color: #333;
    border-radius: 5px;
    padding: 0;
}

.custom-menu a {
    text-decoration:none;
    color:#333;
}


.custom-menu li {
    padding: 8px 12px;
    cursor: pointer;
    list-style-type: none;
}

.custom-menu li:hover {
    background-color: #DEF;
}

这是我的HTML ...

<ul class='custom-menu'>
  <li data-action="first"><a href="/">Home</a></li>
  <li data-action="second"><a href="/profile.php?id=<?php echo $memberID; ?>">Profile</a></li>
  <li data-action="third"><a href="/friends.php">Friends</a></li>
  <li data-action="fourth"><a href="/messages.php">Messages</a></li>
  <li data-action="fifth"><a href="/settings.php">Settings</a></li>
  <li data-action="sixth"><a href="/apps.php">Apps</a></li>
  <li data-action="seventh"><a href="/logout.php">Logout</a></li>
</ul>

我目前的工作原理并且看起来很好。我宁愿保持同样的方式。只是添加了选项。我非常感谢你非常感谢。

2 个答案:

答案 0 :(得分:2)

  

我想添加标准选项,例如检查元素,视图源等,以自定义右键单击...

我们不是全部。 :-)我担心不,这种集成目前不可能。 (有些浏览器甚至不允许你拦截事件。)

*我说“目前”,因为这些天新的界面被迅速地添加到浏览器中,所以最终可能......

答案 1 :(得分:0)

好吧,您可以实施like this:

document.onmousedown = custom_click;
function custom_click(e){
    $("#myList").remove();
    if (e.button == 2) {
        var div = $("<div id='myList'></div>").appendTo("body");
        div.css({
            "top": e.pageY, "left": e.pageX, "z-index": 9999
        });
        div.append("<div>option 1</div>");
        div.append("<div>option 2</div>");
        div.append("<div>option 3</div>");
        div.append("<div>.......</div>");
        return false;
    }
}

使用&#34;检查元素&#34;或其他选项..你可以触发像F12这样的按键。

希望它有所帮助。