【问题标题】:JQuery: All Submenu's expand if a child in one has classJQuery:如果一个孩子有班级,则所有子菜单都会展开
【发布时间】:2017-02-13 11:32:47
【问题描述】:

如果子元素包含“current_page_item”类,我有一小段代码通过将“in”类添加到父级来扩展子菜单。问题是,如果任何子菜单都有该类的子菜单,它们都会扩展。

if ( $('.children li').hasClass('current_page_item') ) {
     $('.children', this)
        .addClass('in');
};

https://jsfiddle.net/n94Lwe9t/

我无法定位 ID 或其他任何特定内容。我将如何定位具有该子类的特定父级?

【问题讨论】:

  • 试试 $('.children>li')
  • 不,没有这样做:( jsfiddle.net/n94Lwe9t/2
  • 试试这个 $('li.current_page_item').parents('ul') .addClass('in')
  • 有效但不能解决字形图标

标签: jquery twitter-bootstrap-3 collapse submenu


【解决方案1】:

遍历children类的子元素li,然后使用closest()找到合适的父元素

$('.children li').each(function(){

    if($(this).hasClass('current_page_item')) {
    console.log($(this).closest('.has-children').find('.list-group-item .glyphicon'));
    $(this).closest('.has-children').find('.list-group-item .glyphicon').toggleClass('glyphicon-chevron-right').addClass('glyphicon-chevron-down')
     $(this).closest('.children').addClass('in');
  }

})

JSFIDDLE

【讨论】:

  • 这很有效,谢谢!我还不是最擅长循环的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-21
  • 2014-05-02
  • 1970-01-01
  • 2013-07-21
  • 1970-01-01
相关资源
最近更新 更多