3

我有一个无序列表,需要拆分为分层列表,具体取决于已预先分配给每个的类

  • . 具体来说,如果原始列表是这样的:

    <ul>
      <li class="level-1"><a href="#">First Level Item</a></li>
      <li class="level-2"><a href="#">Second Level item</a></li>
      <li class="level-3"><a href="#">Third Level item</a></li>
      <li class="level-3"><a href="#">Third Level item</a></li>
      <li class="level-2"><a href="#">Second Level item</a></li>
      <li class="level-3"><a href="#">Third Level item</a></li>
      <li class="level-3"><a href="#">Third Level item</a></li>
      <li class="level-3"><a href="#">Third Level item</a></li>
      <li class="level-2"><a href="#">Second Level item</a></li>
      <li class="level-2"><a href="#">Second Level item</a></li>
      <li class="level-1"><a href="#">Next First Level Item</a></li>
      <li class="level-2"><a href="#">Second Level item</a></li>
      <li class="level-2"><a href="#">Second Level item</a></li>
      <li class="level-3"><a href="#">Third Level item</a></li>
      <li class="level-3"><a href="#">Third Level item</a></li>
    </ul>
    

    我需要它成为基于每个 li 的类的嵌套列表,因此最终列表呈现为:

    <ul>
      <li class="level-1"><a href="#">First Level Item</a>
        <ul>
          <li class="level-2"><a href="#">Second Level item</a>
            <ul>
              <li class="level-3"><a href="#">Third Level item</a></li>
              <li class="level-3"><a href="#">Third Level item</a></li>
            </ul>
          </li>
          <li class="level-2"><a href="#">Second Level item</a>
            <ul>
              <li class="level-3"><a href="#">Third Level item</a></li>
              <li class="level-3"><a href="#">Third Level item</a></li>
              <li class="level-3"><a href="#">Third Level item</a></li>
            </ul>
          </li>
          <li class="level-2"><a href="#">Second Level item</a></li>
          <li class="level-2"><a href="#">Second Level item</a></li>
        </ul>
      </li>
      <li class="level-1"><a href="#">Next First Level Item</a>
        <ul>
          <li class="level-2"><a href="#">Second Level item</a></li>
          <li class="level-2"><a href="#">Second Level item</a>
            <ul>
              <li class="level-3"><a href="#">Third Level item</a></li>
              <li class="level-3"><a href="#">Third Level item</a></li>
            </ul>
          </li>
        </ul>
    </ul>
    

    因为我不能在输出这个列表的 CMS 上运行服务器端代码,所以我必须在客户端重新组织这个 HTML 列表。

    我花了很多时间尝试编写自己的 jQuery 来创建新的层次结构,但运气不佳。我的第一次尝试是尝试使用 jQuery .before</ul></li>在类之前添加附加标签,然后在标签之后附加一个新<ul>标签,但我无法<ul> <li>插入任何标签——似乎 jquery 会添加一个<p>等但是不是</ul></li>吗?

    然后我尝试使用 .wrap<ul></ul>在需要的地方放置一个新的,但我还不够聪明,无法弄清楚如何选择和分组所有子或孙子来包装。

    有人对我应该尝试做什么有任何提示吗?

  • 4

    2 回答 2

    3

    我认为很难操纵现有列表的元素。相反,生成新标记并用它替换现有标记会更容易。

    我可以想出这样的事情:

        var prevlevel = 1;    
        var currentlevel;
        var newlisthtml = "";
    
        $("#idoful li").each(function(){
            currentlevel = parseInt($(this).attr("class").split("-")[1]);
    
            if(currentlevel > prevlevel) {
                newlisthtml += "<ul>";
            } else if(currentlevel < prevlevel) {
                newlisthtml += "</ul></li>";
            } else if(currentlevel == prevlevel) {
                        newlisthtml += "</li>";
                }
    
            newlisthtml += '<li class="level-' + currentlevel + '">' + $(this).html();
            prevlevel = currentlevel; 
        });
    
        $("#idoful").html(newlisthtml);
    

    我使用 jQuery 已经很长时间了。我没有测试上面的代码。将其称为算法而不是代码。你将不得不做这样的事情。

    于 2011-06-05T15:38:27.430 回答
    0

    我会从头开始,然后倒退,以便树木一起移动。

    function nest_list(num) 
    {
        if (num <= 1) { 
            return; 
        } else {
           var item_selector = ".level-" + num.toString();
           var parent_item_selector = ".level-" + (num - 1).toString();
    
           $(item_selector).each(function() {
               $parent_item = $(this).prev(parent_item_selector);
    
               if ($parent_item.length > 0) {
                   // verify the ul
                   var $ul = $parent_item.find('ul');
                   if ($ul.length == 0) {
                      $ul = $('<ul></ul>').appendTo($parent_item);
                   } 
    
                   $ul.append($(this)); // moves the li
               }
           });
    
           nest_list(num - 1);
        }
    );
    
    nest_list(3); // number is the highest level that you want to nest from
    
    于 2011-06-05T18:17:24.850 回答