【问题标题】:jQuery add element in each loopjQuery在每个循环中添加元素
【发布时间】:2019-11-07 12:24:23
【问题描述】:

在尝试为每个包含特定类的元素添加切换按钮时,我对 jQuery 感到有些头疼。 当我使用 jQuery 的 .each( 时,我希望在循环中添加我的标识符类。 但不知何故,它不断将我的html代码循环附加到每个li而不是li.has-children

这是我当前的代码:

    function addLevelClass($parent, level) {
      // fetch all the li's that are direct children of the ul
      var $children = $parent.children('li');
      // loop trough each li
      $children.each(function() {
        // get the ul that is a direct child of the li
        var $sublist = $(this).children('ul');
        // if an ul was found
        if ($sublist.length > 0) {
          $sublist.addClass('slideable-submenu');
          // add a class to the current li indicating there is a sub list
          $(this).addClass('has-children level-'+level);


          //last attempt before ask on SO
          if( $(this).hasClass('has-children level-'+level) ){
            $( 'li.has-children span a' ).after( '<span class="sub-menu-toggle"></span>');
          }

          // repeat the process for the sublist, but with the level one higher
          // = recursive function call
          addLevelClass($sublist, level+1);
        }
      });
    }

    // call the function to add level classes on the upper most ul
    addLevelClass($('.header-mobile-menu'), 0);
    //$( 'li.has-children span a' ).after( '<span class="sub-menu-toggle"></span>');//Adds toggle buttons everywhere

所以我们的想法是:

$( 'li.has-children span a' ).after( '<span class="sub-menu-toggle"></span>');

在正确的位置。

【问题讨论】:

  • 你能张贴标记吗?你也不需要if( $(this).hasClass('has-children level-'+level) ),因为它总是正确的
  • @SeanT ,感谢您的帮助尝试(小提琴很痛苦,答案来得更快)。我做了很多尝试,例如:find('a'),但应该是find('span:first')。我们不断学习。

标签: jquery each


【解决方案1】:

如果我理解正确,您正在尝试为每个具有子菜单的 &lt;li&gt; 添加一个切换按钮。

如果是这样,我创建了一个 fiddle 并带有一些可能有用的通用标记。

我所做的唯一真正的改变是如何附加切换按钮,并且我删除了递归调用自身。

这是更新后的代码:

function addLevelClass($parent, level) {

      // fetch all the li's that are direct children of the ul
      var $children = $parent.children('li');

      // loop trough each li
      // here I added a check if level is defined, if not set it to 0, this way you don't have to pass it a value unless you want to start it somewhere
      var level = (typeof(level) !== 'undefined') ? level : 0;
      $children.each(function() {
        // get the ul that is a direct child of the li
        var $sublist = $(this).children('ul');
        // if an ul was found
        if ($sublist.length > 0) {


          $sublist.addClass('slideable-submenu');

          // add a class to the current li indicating there is a sub list
          $(this).addClass('has-children level-'+level).find('span:first').append( '<span class="sub-menu-toggle">toggle</span>');

          // increment level
          level++;

        }
      });
    }

    // call the function to add level classes on the upper most ul
    addLevelClass($('#parent ul'));
    //$( 'li.has-children span a' ).after( '<span class="sub-menu-toggle"></span>');//Adds toggle buttons everywhere

【讨论】:

  • 如果我保持递归调用它就可以工作,似乎我需要的是:$(this).addClass('has-children level-'+level).find('span:first').append( '&lt;span class="sub-menu-toggle"&gt;toggle&lt;/span&gt;');
  • 我必须保持递归调用的原因是它是通过 php 生成的多级菜单。好工作!!!答案已接受。
  • 很高兴能帮上忙!
猜你喜欢
  • 2012-11-21
  • 2017-10-07
  • 1970-01-01
  • 2018-05-18
  • 2015-11-01
  • 2023-04-03
  • 2015-08-19
  • 2017-02-12
  • 2011-07-20
相关资源
最近更新 更多