简体   繁体   中英

Add standard right-click options to custom right-click

I would like to add standard options, such as inspect element, view source, etc. to custom right-click if this is possible.

Here's my 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>

Here's my 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;
}

Here's my 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>

What I have currently works and is looking good. I would prefer to keep it the same way. Just with the added options. I would greatly appreciate it Thank You very much.

I would like to add standard options, such as inspect element, view source, etc. to custom right-click...

Wouldn't we all. :-) I'm afraid that no, this kind of integration is not currently* possible. (Some browsers don't even let you intercept the event.)


* I say "currently" because new interfaces are being added to browsers fairly rapidly these days, so maybe eventually...

Well, you can implement 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;
    }
}

To use "inspect element" or other options.. you can trigger a key press like F12.

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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