jQuery.在两个相互叠加的元素上绑定上下文菜单事件
jQuery.bind context menu event on two elements overlaid on top of eachother
我有两个相互顶部的divs
。我想要的是,当底部div
被点击一个上下文菜单出现,当我点击上部div
我想要一个不同的上下文菜单出现。divs
需要保持在彼此的顶部。
我遇到的问题是,当我单击上部div
时,绑定事件执行两次。我相信这是因为它们是相互叠加的,两个divs
都有一个绑定事件附加到它们。
是否有一种方法,只有绑定事件执行一次单击div
?基本上优先考虑最上面的(Z
轴)div
。
我有一个例子,你可以看到,当点击红色的div
时,attributeMenu
上下文菜单出现,但当你点击绿色的div
时,attributeMenu and elementMenu
上下文菜单出现。
下面是示例jsfiddle
部分代码:
addElementMenu();
addAttributeMenu();
// 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 "duplicate":
duplicateItem(clicked);
break;
case "delete":
deleteItem(clicked);
break;
case "copy":
copyItem(clicked);
break;
case "cut":
cutItem(clicked);
break;
case "paste":
pasteItem();
break;
}
// Hide it AFTER the action was triggered
$(".custom-menu").hide(100);
});
function addElementMenu() {
$('.element').bind("contextmenu", function (event) {
// Avoid the real one
event.preventDefault();
//set clicked item
clicked = $(this);
parent = $(this).parent().attr('id');
console.log(clicked);
console.log(parent);
// Show contextmenu
$("#elementMenu").finish().show().css({
top: event.pageY + "px",
left: event.pageX + "px",
display: 'block'
});
});
}
function addAttributeMenu() {
$('.attribute').bind("contextmenu", function (event) {
// Avoid the real one
event.preventDefault();
//set clicked item
clicked = $(this);
parent = $(this).parent().attr('id');
console.log(clicked);
console.log(parent);
// Show contextmenu
$("#attributeMenu").finish().show().css({
top: event.pageY + "px",
left: event.pageX + "px",
display: 'block'
});
});
}
CSS: .attribute {
width: 400px;
height: 200px;
background-color: red;
}
.element {
width: 200px;
height: 100px;
background-color: green;
}
.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 li {
padding: 8px 12px;
cursor: pointer;
list-style-type: none;
}
.custom-menu li:hover {
background-color: #DEF;
}
HTML: <div class='attribute' id="attributeID">
<div class='element' id="elementID"></div>
</div>
<ul class='custom-menu' id="elementMenu">
<li class='visibleElement' data-action="duplicate">Duplicate</li>
<li class='visibleElement' data-action="delete">Delete</li>
<li class='visibleElement' data-action="copy">Copy</li>
<li class='visibleElement' data-action="cut">Cut</li>
<li class='visibleAttribute' data-action="paste">Paste</li>
</ul>
<ul class='custom-menu' id='attributeMenu'>
<li class='visibleAttribute' data-action="paste">Paste</li>
</ul>
使用stopPropagation()
,这样当您右键单击子div时不会触发父div中的事件。
脚本:
function addElementMenu() {
$('.element').bind("contextmenu", function (event) {
// Avoid the real one
event.preventDefault();
// Avoid the event from bubbling up to parent
event.stopPropagation();
//set clicked item
clicked = $(this);
parent = $(this).parent().attr('id');
console.log(clicked);
console.log(parent);
// Show contextmenu
$("#elementMenu").finish().show().css({
top: event.pageY + "px",
left: event.pageX + "px",
display: 'block'
});
});
}
小提琴
相关文章:
- 将动态元素绑定到函数;只剩下一个绑定
- Knockout绑定是膨胀的html(表单元格),如何使用javascript创建绑定或从父元素绑定
- 使用Kendo的动态HTML元素绑定
- 将多个元素绑定到同一onclick
- 如何在AJAX驱动的应用程序中优化元素绑定
- 使用AngularJS UI路由器时出现元素绑定问题
- 如何将 DOM 元素绑定到自定义 $.touchpress 事件
- 如何在React Native中为ListView中的每个元素绑定函数
- 对克隆的image元素绑定onclick事件
- 将DIV元素绑定到溢出滚动条
- 使用ng repeat将表单元素绑定到角度中的新对象
- 将元素绑定到不同帧中的现有 AngularJS 作用域
- KNOCKOUTJS 将多个输入元素绑定到一个可观察量
- 如何将两个 html 元素绑定在一起,当第一个元素被删除时,第二个元素也从 DOM 中删除
- OOJS-将每个元素绑定到一个特定的点击
- 真正阻止元素绑定-取消绑定元素-AngularJS
- 如何在jQuery中将多个元素绑定到多个事件
- Vue.js-将同名的无线电元素绑定到数组
- 将两个数组元素绑定为一个
- Flotr2 -安全地销毁图形元素以及为该元素绑定的所有事件