简体   繁体   English

jQuery扩展导航在儿童点击时折叠

[英]jQuery Expanding Nav Collapsing on Child Click

https://jsfiddle.net/aesya0g6/ https://jsfiddle.net/aesya0g6/

Hello! 你好!

I have built an expanding nav, and it is running exactly how I want, except for the fact that it is collapsing on the click of the children. 我已经构建了一个扩展的导航,它正在按照我想要的方式运行,除了它在孩子们点击时崩溃的事实。 I have tried switching where the jQuery is pointing, in hopes that this would resolve it, but have had no luck. 我试过切换jQuery指向的地方,希望这会解决它,但没有运气。 Where am I going wrong? 我哪里错了?

    $('.expandingNav').click(function(){
  if($(this).hasClass('expanded')){
    $(this).children('.hidden').slideUp();
    $(this).removeClass('expanded');
  }
  else{
    $(this).children('.hidden').slideDown();
    $(this).addClass('expanded');
  }
});

You could check to see if the closest ul element has a parent element of nav.sidebar : 您可以检查最近的ul元素是否具有nav.sidebar的父元素:

$('.expandingNav').on('click', function (e) {
    if ($(e.target).closest('ul').parent('nav.sidebar').length) {
        // ...
    }
});

Updated Example 更新的示例

$('.expandingNav').on('click', function (e) {
    if ($(e.target).closest('ul').parent('nav.sidebar').length) {
        if ($(this).hasClass('expanded')) {
            $(this).children('.hidden').slideUp();
            $(this).removeClass('expanded');
        } else {
            $(this).children('.hidden').slideDown();
            $(this).addClass('expanded');
        }
    }
});

Alternatively, you could also check to see if the parent element's next sibling is a ul element: 或者,您还可以检查父元素的下一个兄弟是否是ul元素:

$('.expandingNav').on('click', function (e) {
    if ($(e.target).parent().next('ul').length) {
        // ...
    }
});

Updated Example 更新的示例

$('.expandingNav').on('click', function (e) {
    if ($(e.target).parent().next('ul').length) {
        if ($(this).hasClass('expanded')) {
            $(this).children('.hidden').slideUp();
            $(this).removeClass('expanded');
        } else {
            $(this).children('.hidden').slideDown();
            $(this).addClass('expanded');
        }
    }
});

You could simple achieve it by adding: 你可以通过添加:

//If the e.target is the same element as this, you've not clicked on a descendant.
if( e.target !== this ) return;

updated fiddle 更新小提琴

When you click on an item's child the click event "bubbles up" to its parent thus triggering the click event which causes the slide up. 当您单击某个项目的子项时,单击事件会“冒泡”到其父项,从而触发单击事件,从而导致向上滑动。 You should handle the click event on the children and use stopPropagation to avoid event bubbling. 您应该处理子项上的click事件并使用stopPropagation来避免事件冒泡。 Try something like this: 尝试这样的事情:

$('.expandingNav li').click(function(ev) { ev.stopPropagation(); })

You shuold Avoid propagation anything Inside the element you click here in this case its li inside class expandingNav . 您shuold避免传播任何的元素在里面你点击这里在这种情况下,其li类中expandingNav

   $("li.expandingNav").on('click', function(e) {
  if($(this).hasClass('expanded')){
    $(this).children('.hidden').slideUp();
    $(this).removeClass('expanded');
  }
  else{
    $(this).children('.hidden').slideDown();
    $(this).addClass('expanded');
  }
    });
//On sub LI click , stop propagating. 
$("li.expandingNav li").on('click', function(e) {
 e.stopPropagation();
}); 

Working Fiddle 工作小提琴

dont bother yourself with confusing extra codes. 不要为困扰额外的代码而打扰自己。

just put the listener on the a tag. 只需将监听器放在标签上即可。

like this: 像这样:

  $('.expandingNav').children('a').click(function(){
   var element = $(this).parent();
  if($(element).hasClass('expanded')){
    $(element).children('.hidden').slideUp();
    $(element).removeClass('expanded');
  }
  else{
    $(element).children('.hidden').slideDown();
    $(element).addClass('expanded');
  }
});

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

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