0
問題:上下文菜單正常工作,但如果您在多個div上拉出上下文菜單,它會記住點擊事件。使菜單被拉起的部分特定的上下文菜單事件
要重新創建:右鍵單擊一個部分。點擊菜單外部關閉它(不要選擇一個選項)。點擊另一部分並選擇一個選項。如果刪除,它將刪除菜單被拉出的兩個部分。如果您選擇向下移動該部分,則會移動菜單拉出的第一部分。
問題:如何優化下面的JS/Jquery來保持菜單事件的菜單被拉起的部分?
$(".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);
}
}
});
});