gpt4 book ai didi

jquery - 嵌套子元素上的上下文菜单还显示父上下文菜单

转载 作者:行者123 更新时间:2023-12-03 23:04:27 24 4
gpt4 key购买 nike

我有多个带有上下文菜单的 DOM 元素。当一个元素是另一个元素的子元素并且我调用内部子元素的上下文菜单时,我还会看到父元素的上下文菜单。这是通过 jquery-ui.contextmenu 实现的插件。

有没有办法配置插件以防止显示父菜单,或者我是否必须手动处理所有点击事件并过滤它们,以便只显示我想要的菜单?

以下是我的代码:

HTML:

    <!-- Add a child which will have a context menu -->
<div class="outer-child" id="outer-child">
Outer Child

<!-- inner child with its own context menu -->
<div class="inner-child" id="inner-child">
Inner Child
</div>
</div>
</div>

CSS:

.outer-child {
position: absolute;
top: 0px;
left: 0px;
width: 200px;
height: 200px;
border: 1px solid red;
background: green;
}
.inner-child {
position: absolute;
top: 50px;
left: 50px;
width: 100px;
height: 100px;
border: 1px solid blue;
background: yellow;
}

JavaScript:

// create context menu on outer child
$("#outer-child").contextmenu({
menu: [
{title: "This is the Outer Menu", cmd: "outer-menu"}
],
select: function(event, ui) {
alert("select " + ui.cmd + " on " + ui.target.text());
}
});


// create context menu on inner child
$('#inner-child').contextmenu({
menu: [
{title: "Inner Menu", cmd: "inner-menu"}
],
select: function(event, ui) {
alert("select " + ui.cmd + " on " + ui.target.text());
}
});

您可以找到 jsfiddle 演示 here 。 (右键单击内部框并查看两个上下文菜单)

最佳答案

您可以通过在子元素的 beforeOpen 事件中调用 event.stopPropagation() 方法来解决此问题。

// create context menu on outer child
$("#outer-child").contextmenu({
menu: [{
title: "This is the Outer Menu",
cmd: "outer-menu"
}],
select: function(event, ui) {
alert("select " + ui.cmd + " on " + ui.target.text());
},

});


// create context menu on inner child
$('#inner-child').contextmenu({
beforeOpen: function(event, ui) {
event.stopPropagation();
},
menu: [{
title: "Inner Menu",
cmd: "inner-menu"
}],
select: function(event, ui) {
alert("select " + ui.cmd + " on " + ui.target.text());
}
});
.outer-child {
position: absolute;
top: 0px;
left: 0px;
width: 200px;
height: 200px;
border: 1px solid red;
background: green;
}
.inner-child {
position: absolute;
top: 50px;
left: 50px;
width: 100px;
height: 100px;
border: 1px solid blue;
background: yellow;
}
<link href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<script src="http://wwwendt.de/tech/demo/jquery-contextmenu/jquery.ui-contextmenu.js"></script>
<!-- Create an area to contain our editable components -->
<div class="container" id="container">
<!-- Add a child which will have a context menu -->
<div class="outer-child" id="outer-child">Outer Child
<!-- inner child with its own context menu -->
<div class="inner-child" id="inner-child">Inner Child</div>
</div>
</div>

关于jquery - 嵌套子元素上的上下文菜单还显示父上下文菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26533140/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com