Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript JQuery删除不删除空列表元素_Javascript_Jquery - Fatal编程技术网

Javascript JQuery删除不删除空列表元素

Javascript JQuery删除不删除空列表元素,javascript,jquery,Javascript,Jquery,我编写了下面的方法来平展嵌套列表的深层层次结构(我使用的是TypeScript,但您可以理解JS) 该方法针对每个顶级元素运行,以将其所有子元素插入其后面: // Flatten a nested list branch - acts in reverse to allow for nested lists _flattenBranch(element: HTMLElement) { var el = $(element); $(el.fi

我编写了下面的方法来平展嵌套列表的深层层次结构(我使用的是TypeScript,但您可以理解JS)

该方法针对每个顶级
  • 元素运行,以将其所有子元素
  • 插入其后面:

        // Flatten a nested list branch - acts in reverse to allow for nested lists
        _flattenBranch(element: HTMLElement)
        {
            var el = $(element);
            $(el.find('li').get().reverse()).each(function ()
            {
                el.after(this);
            });
            // FIX: This is not removing empty descendant lists!
            el.find('ul').remove();
        }
    
    我注意到在查看生成的输出时,在一些
  • 下仍然有空的
      s。我错过了什么

      更新(和复制):

      试试这个JSFIDLE: 这将导致输出中出现以下错误:

      <li>Application Form
          <ul>
          </ul>
      </li>
      

      这是一个简单的解决方案,但前提是您有3个级别的列表:

      function test(element){
          var el = $(element);
          $(el.find('li').get().reverse()).each(function () {
              el.after(this);
              $(this).find('ul').remove();
          });
          // FIX: This is not removing empty descendant lists!
          el.find('ul').remove();
      }
      $('#test').children('li').each(function(){
          test(this)
      });
      

      在您的示例中,您尝试在家庭的孩子中查找所有
      ,但是,申请表不再是家庭的孩子

      对我来说似乎很好,如果你通过ul功能,你能在演示中重现问题吗..你应该像Arun那样做…我现在正在研究具体问题。。。它似乎只影响某些子列表,因此问题可能出在其他地方。谢谢你的例子。很高兴知道我没有发疯。@Arun P Johny:我设法重现了这个问题,用我稍微不同的调用代码替换到您的JSFIDLE中。为什么它可以工作,但如果它使用
      children()
      ,却不能删除所有的
      子项?
      function test(element){
          var el = $(element);
          $(el.find('li').get().reverse()).each(function () {
              el.after(this);
              $(this).find('ul').remove();
          });
          // FIX: This is not removing empty descendant lists!
          el.find('ul').remove();
      }
      $('#test').children('li').each(function(){
          test(this)
      });