使上下文菜单事件特定于菜单被拉出的部分
Make context menu events specific to the section the menu was pulled up
问题:上下文菜单正在运行,但如果您在多个div上调出上下文菜单,它会记住单击事件。
要重新创建:右键单击一个部分。单击菜单外部以关闭它(不要选择选项)。单击另一个部分并选择一个选项。如果删除,它将删除菜单所指向的两个部分。如果您选择上下移动该部分,它将移动上拉菜单的第一个部分。
问题:如何优化以下JS/Jquery,使菜单中的事件保持特定于弹出菜单的部分?
JS Fiddle
$(".templateSection").contextmenu(function (event) {
var thisTemplate = this;
// Avoid the real one
event.preventDefault();
// Show contextmenu
$(".custom-menu").toggle(100).
// In the right position (the mouse)
css({
top: event.pageY + "px",
left: event.pageX + "px"
});
$(".editArea").css('outline', 'none')
//Delete section menu button
$("#menuDelete").click(function () {
$(thisTemplate).remove();
$(".custom-menu").hide(100);
});
//Move sections up and down
function firstAndLast(container) {
firstAndLast($('#container'));
}
$('.menuUp, .menuDown').click(function (e) {
e.preventDefault();
var parent = $(thisTemplate).closest('.templateSection'),
grandparent = $(thisTemplate).closest('#container');
if ($(this).hasClass('menuUp')) {
parent.insertBefore(parent.prev('.templateSection'));
firstAndLast(grandparent);
} else if ($(this).hasClass('menuDown')) {
parent.insertAfter(parent.next('.templateSection'));
thisTemplate.clearQueue();
}
});
// Hide menu if the document is clicked somewhere
$(document).bind("mousedown", function (e) {
//left mouse down
switch (e.which) {
case 1:
// If the clicked element is not the menu
if (!$(e.target).parents(".custom-menu").length > 0) {
// Hide it
$(".custom-menu").hide(100);
}
}
});
});
将JS更改为以下代码。如果给出了更优化的解决方案,我将更改已接受的答案。
JS Fiddle
//Replace default context menu on right click
$(function () {
$('.custom-menu').hide();
$(".templateSection").contextmenu(function (e) {
e.preventDefault();
thisTemplate = this;
$(".custom-menu").css({
top: event.pageY + "px",
left: event.pageX + "px"
}).fadeIn(200);
});
});
//Delete Section
$("#menuDelete").click(function () {
$(thisTemplate).remove();
$(".custom-menu").hide(100);
});
//Move sections up and down
$('.menuUp, .menuDown').click(function () {
var parent = $(thisTemplate).closest('.templateSection');
if ($(this).hasClass('menuUp')) {
parent.insertBefore(parent.prev('.templateSection'));
} else if ($(this).hasClass('menuDown')) {
parent.insertAfter(parent.next('.templateSection'));
}
});
$(document).mousedown(function (e) {
//left mouse down
switch (e.which) {
case 1:
// If the clicked element is not the menu
if (!$(e.target).parents('.custom-menu').length > 0) {
$(".custom-menu").hide(100);
}
}
});
相关文章:
- 使用java脚本更改onClick事件中菜单选项卡的颜色
- 下拉菜单大小已更改事件
- jQuery与导航菜单上的mouseover事件冲突.
- 单击事件打开两个弹出菜单
- Javascript问题与下拉菜单上的单击事件有关
- 响应菜单问题-如何在移动环境中处理HREF和onclick事件
- 关闭 jQuery 中的菜单事件
- API Google Maps Javascript在上下文菜单中调用事件侦听器
- 如何在Jquery mobile中处理动态选择菜单事件
- 引导明细菜单:跳回主菜单事件
- 自定义下拉菜单,何时触发隐藏菜单事件
- 在Mac版Safari中使用Ctrl+click时,如何从上下文菜单事件禁用单击事件
- 在ListViewItem上单击鼠标右键和上下文菜单事件
- 使上下文菜单事件特定于菜单被拉出的部分
- 在未按预期工作的文档上右键单击(上下文菜单)事件上创建弹出式菜单
- 基础6下拉菜单事件不触发
- jQuery.在两个相互叠加的元素上绑定上下文菜单事件
- 如何检查上下文菜单事件是从鼠标或键盘触发的jquery
- 当拼写检查打开时,上下文菜单事件在ie上不起作用
- 下拉菜单事件侦听器