jQuery   发布时间:2022-04-19  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了jquery – 嵌套子元素上的上下文菜单也显示父上下文菜单大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有多个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"}
        ],ui) {
        alert("SELEct " + ui.cmd + " on " + ui.target.text());
    }
});

你可以找到一个jsfiddle demo here.(右键单击内框并查看两个上下文菜单)

解决方法

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

// create context menu on outer child
$("#outer-child").contextmenu({
  menu: [{
    title: "This is the Outer Menu",cmd: "outer-menu"
  }],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"
  }],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 – 嵌套子元素上的上下文菜单也显示父上下文菜单全部内容,希望文章能够帮你解决jquery – 嵌套子元素上的上下文菜单也显示父上下文菜单所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。