如何在HTML中的li项目上创建右键单击自定义菜单

时间:2013-10-07 10:59:17

标签: javascript jquery html css

我的要求是我想在特定的li项目上打开右键自定义菜单。我从

获得了一个js文件

http://lablogic.net/index.php#scripts/contextmenu/right-click-menu.php

URL但在此js文件中右键单击适用于整个页面,但我希望它只能使用某些li或标签。当用户右键单击特定的li或标签时,此右键单击菜单会打开,如果用户点击其他任何位置,则会隐藏并打开正常的右键单击菜单。

请帮帮我.....

供参考..例如http://www.dropbox.comhttp://skydrive.live.com等......

2 个答案:

答案 0 :(得分:2)

对于初学者来说,我并不确切知道你想要达到哪种效果,但是当你点击文件和目录时,我会认为这是一个类似于skydrive的菜单。

可能有很多解决方案,但我可以坚持使用你的代码。首先要做的是 - 我们需要解码它。你可以使用像这样的一些在线beutyfiers:http://jsbeautifier.org/

我已经复制了文件contextmenu_o.js的代码并获得了此代码:

var mouse_x = 0;
var mouse_y = 0;
var mouse_left = false;
var mouse_right = false;
if (document.addEventListener != undefined) document.addEventListener('mousemove', mouseMove, true);
else if (document.layers) document.captureEvents(Event.MOUSEDOWN | Event.MOUSEMOVE | Event.MOUSEUP);
document.onmousemove = mouseMove;
document.oncontextmenu = RightMouseDown;
document.onmousedown = mouseDown;
document.onmouseup = mouseUp;

function mouseMove(a) {
    mouse_x = document.all ? event.clientX + document.body.scrollLeft : document.layers ? a.x + window.pageXOffset : a.clientX + window.pageXOffset;
    mouse_y = document.all ? event.clientY + document.body.scrollTop : document.layers ? a.y + window.pageYOffset : a.clientY + window.pageYOffset
}

function RightMouseDown() {
    mouse_right = true;
    return false
}

function init(a, w) {
    var b = document.createElement("DIV");
    b.id = "contextmenu";
    if (!w) var w = 120;
    b.style.width = w + "px";
    var c = '<div style="position:relative;left:10px;top:-4px;">';
    c += a;
    c += ' <a href="http://lablogic.net/scripts/contextmenu/right-click-menu.php" target="_top" title="javascript contextmenu" alt="Free javascript menu"><font color="#565656" size="-2">Get it...</font></a>';
    c += '</div>';
    b.innerHTML = c;
    b.style.position = "absolute";
    b.style.left = "0px";
    b.style.top = "0px";
    b.style.visibility = "hidden";
    b.style.overflow = "hidden";
    b.style.padding = "4px";
    b.style.backgroundColor = "#ffffff";
    b.style.border = "1px solid #6a6868";
    document.body.appendChild(b);
    delete b
}

function mouseUp(e) {
    if (e.which == 1) document.getElementById("contextmenu").style.visibility = "hidden";
    else if (e.which == 3) mouse_right = false
}

function mouseDown(e) {
    if (e.which == 3) {
        mouse_right = true;
        document.getElementById("contextmenu").style.left = mouse_x + "px";
        document.getElementById("contextmenu").style.top = mouse_y + "px";
        document.getElementById("contextmenu").style.visibility = "visible"
    }
}

这不是最通用的代码,我们需要进行一些更改。我在 jsfiddle 上显示我的更改,但主要是我用我选择的div覆盖文档,我正在给事件监听器。

答案 1 :(得分:0)

具有基于li列表的下拉菜单的按钮。

jsfiddle.net

点击按钮打开。

var $menuButton = $("#menuButton");
var $menuElement = $("#menuElement");

$menuButton.button({
    icons: {
        secondary: "ui-icon-triangle-1-s"
    }
})
    .click(function (event) {
    $(document).one("click", function () {
        $menuElement.css("visibility", "hidden");
    });

    $menuElement.css("visibility", "visible");
    event.stopPropagation();
});

$menuElement
    .menu({
    select: function (event, ui) {
        $menuElement.css("visibility", "hidden");
    }
})
    .css("visibility", "hidden")
    .position({
    my: "left top",
    at: "left bottom",
    of: $buttonElement
});