Creating dropdown menu from simple list

后端 未结 3 945
时光取名叫无心
时光取名叫无心 2021-01-06 12:18

I have current list look like:

相关标签:
3条回答
  • 2021-01-06 12:28

    $('.group1').wrapAll('<ul></ul>');
    $('.group2').wrapAll('<ul></ul>');
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <ul>
      <li><a href="#">Menu 1</a>
      </li>
      <li class='group1'><a href="#">_Submenu a</a>
      </li>
      <li class='group1'><a href="#">_Submenu b</a>
      </li>
      <li class='group1'><a href="#">_Submenu c</a>
      </li>
      <li><a href="#">Menu 2</a>
      </li>
      <li><a href="#">Menu 3</a>
      </li>
      <li class='group2'><a href="#">_Submenu x</a>
      </li>
      <li class='group2'><a href="#">_Submenu y</a>
      </li>
      <li><a href="#">Menu 4</a>
      </li>
    </ul>

    Try this way

    Use .wrap()

    Description: Wrap an HTML structure around each element in the set of matched elements.

    Use .wrapAll()

    Description: Wrap an HTML structure around all elements in the set of matched elements.

    0 讨论(0)
  • 2021-01-06 12:30

    You can achieve the effect you're looking for by using the following logic.

    1. Create a common cache variable to hold the previous top-level menu.
    2. Loop through all menu list items checking to see whether the text begins with an underscore.
      • If it does, append this to the previous top-level menu.
      • If it does not, append a new unordered list element to this list item and cache the new list in the previous top-level menu variable.
    3. Once the loop has finished you can select all lists that are empty and remove them (.find('ul:empty').remove()).

    In the example below I have favoured Native DOM API methods instead of their jQuery counterparts in a few instances because:

    1. $(this).append('<ul></ul>') returns $(this) instead of the newly created list. The work around is to add another line of code, or just use the DOM API this.appendChild($('<ul>')[0]) which does return the newly created list. And...
    2. It just seems wasteful to use jQuery in situations where it is just as simple to use the DOM API. see: youmightnotneedjquery.com

    var prev;
    $('.menu li').each(function(){
        if(/^_/.test(this.textContent) && prev) {
            prev.appendChild(this);
        } else {
            prev = this.appendChild($('<ul>')[0]);
        }
    }).find('ul:empty').remove();
    <script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
    <ul class="menu">
       <li><a href="#">Menu 1</a></li>
       <li><a href="#">_Submenu a</a></li>
       <li><a href="#">_Submenu b</a></li>
       <li><a href="#">_Submenu c</a></li>
       <li><a href="#">Menu 2</a></li>
       <li><a href="#">Menu 3</a></li>
       <li><a href="#">_Submenu x</a></li>
       <li><a href="#">_Submenu y</a></li>
       <li><a href="#">Menu 4</a></li>
    </ul>

    The example above results in the following HTML structure:

    <ul class="menu">
        <li><a href="#">Menu 1</a>
            <ul>
                <li><a href="#">_Submenu a</a></li>
                <li><a href="#">_Submenu b</a></li>
                <li><a href="#">_Submenu c</a></li>
            </ul>
        </li>
        <li><a href="#">Menu 2</a></li>
        <li><a href="#">Menu 3</a>
            <ul>
                <li><a href="#">_Submenu x</a></li>
                <li><a href="#">_Submenu y</a></li>
            </ul>
        </li>
        <li><a href="#">Menu 4</a></li>
    </ul>
    
    0 讨论(0)
  • 2021-01-06 12:40

    // Create custom selectors
    $.extend($.expr[':'], {
      startsWith: function(e, i, m) {  
        return $(e).text().trim().indexOf(m[3]) === 0;
      }
    });
    
    $("li:not(:startsWith(_))").each(function(){  // LI that are not _Sub
      if($(this).next("li:startsWith(_)").length) // If my next() is _Sub, start grouping:
      $("<ul/>", {
        html: $(this).nextUntil("li:not(:startsWith(_))"),
        appendTo: this
      });
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <ul>
      <li><a href="#">Menu 1</a></li>
      <li><a href="#">_Submenu a</a></li>
      <li><a href="#">_Submenu b</a></li>
      <li><a href="#">_Submenu c</a></li>
      <li><a href="#">Menu 2</a></li>
      <li><a href="#">Menu 3</a></li>
      <li><a href="#">_Submenu x</a></li>
      <li><a href="#">_Submenu y</a></li>
      <li><a href="#">Menu 4</a></li>
    </ul>

    Here's the HTML result:

    <ul>
      <li>
        <a href="#">Menu 1</a>
        <ul>
          <li><a href="#">_Submenu a</a></li>
          <li><a href="#">_Submenu b</a></li>
          <li><a href="#">_Submenu c</a></li>
        </ul>
      </li>
      <li>
        <a href="#">Menu 2</a>
      </li>
      <li>
        <a href="#">Menu 3</a>
        <ul>
          <li><a href="#">_Submenu x</a></li>
          <li><a href="#">_Submenu y</a></li>
        </ul>
      </li>
      <li>
        <a href="#">Menu 4</a>
      </li>
    </ul>
    
    0 讨论(0)
提交回复
热议问题