我尝试使用下面的代码设置仅在单击容器 div 时显示的上下文菜单。但我很混乱。如果它在这里有帮助,那就是小提琴http://jsfiddle.net/PhilippB/SMKMW/1/。
var container = document.getElementById("container");
var contextmenu = document.getElementById("contextmenu");
container.onclick = function() {contextmenu()} ;
contextmenu.style.display = "none";
function contextmenu(event) {
if (contextmenu.style.display == "none") {
contextmenu.style.display = "block";
contextmenu.style.left = event.pageX + "px";
contextmenu.style.top = event.pageY + "px";
}
else {
contextmenu.style.display = "none";
}
}