简体   繁体   English

jQuery Toggle父/子标签

[英]jQuery Toggle parent/child tags

I'm attempting to build a jQuery/CSS mobile menu which has expand buttons for the child ul/li tags. 我正在尝试构建一个jQuery / CSS移动菜单,该菜单具有针对子ul / li标签的展开按钮。

The expand button works fine for "parent > child" items, but not for "parent > child > child" items or any other nested items, when I click on the expand button for a child it closes the parent. 当我单击子项的扩展按钮时,它会关闭父项,而“父项>子项”项对“扩展”按钮的效果很好,但对“父项>子项>子项”项或任何其他嵌套项则无效。

The code I'm working with is here: http://jsfiddle.net/tbxw39hq/ 我正在使用的代码在这里: http : //jsfiddle.net/tbxw39hq/

// Mobile Menu Toggle
$(".mobile-menu li").click(function() {
    $(".sub-menu", this).toggle();
});

// Add Expand To Mobile Menu
$('.mobile-menu li:has(ul.sub-menu)').append('<div class="menu-expand">+</div>');

Any help much appreciated! 任何帮助,不胜感激!

Thanks 谢谢

You need to stop event propagation to child elements: 您需要停止将事件传播到子元素:

$(".mobile-menu li").click(function(e) {
    e.stopPropagation();
    $(">.sub-menu", this).toggle();
});

Working Demo 工作演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM