【问题标题】:If submenu li has more than 5 li's create a new ul and place remaining list items?如果子菜单 li 有超过 5 li 的创建一个新的 ul 并放置剩余的列表项?
【发布时间】:2015-07-27 13:28:13
【问题描述】:

我基本上是在尝试检查我的子菜单是否有超过五个列表项,以及它是否确实抓住了剩余的列表项并将它们放置在使用 jquery 的当前父 ul 之外的新 ul 中。由于列表的结构,它变得复杂。

这是 DOM 结构:

<ul id="nav" class="se test">
<li id="menu1" class="page-1307 parent-menu parent">
    <div class="nav-inner">
        <a class="menulink" id="menuitem1" onclick="return false" href="#">test<span class="toggle"></span></a>
        <ul id="ie1" class="plain">
            <li class="parent-menu parent"><a href="test">test<span class="toggle"></span></a>
                <div class="submenu-wrapper">
                    <ul class="plain">
                        <li><a href="test">test</a></li>
                        <li><a href="test">test</a></li>
                        <li><a href="test">test</a></li>
                        <li><a href="test">test</a></li>
                        <li><a href="test">test</a></li>
                        <li><a href="test">test</a></li>
                        <li><a href="test">test</a></li>
                    </ul>
                </div>
           </li>
           <li class="parent-menu parent"><a href="test">test<span class="toggle"></span></a>
                <div class="submenu-wrapper">
                    <ul class="plain">
                        <li><a href="test">test</a></li>
                        <li><a href="test">test</a></li>
                        <li><a href="test">test</a></li>
                        <li><a href="test">test</a></li>
                    </ul>
               </div>
           </li>
        </ul>
    </div>
</li>
<li id="menu2" class="menulink page-7">
    <div class="nav-inner">
        <a href="test">test</a>
    </div>
</li>
</ul>

基本上我需要获取那些剩余的列表项并将它们放在一个新的 li.parent-menu.parent 中,其中包括子 div.sub-menu-wrapper 和 ul.plain。实际剩余的列表项将进入新的 l​​i.parent-menu 的 ul.plain。我希望从那以后就可以了。我已经坚持了一两天,无法弄清楚。任何帮助将不胜感激,谢谢。

这就是我所追求的,记住它是动态的。

【问题讨论】:

  • 当然可以,到目前为止您尝试过什么?
  • DOM 中是否有新的父 ul 的用途?如果严格用于样式目的,您可能会使用 nth-child css 解决方案。
  • 我已经尝试在子菜单包装器中遍历每个 li 并检查 li.length 是否大于 5,然后抓取剩余的列表项并将它们推送到新数组.获取数组并将其放置在由 .submenu-wrapper 和 ul.plain 包装的新创建的 li.parent-menu 中。我感到沮丧并从头开始,否则我会发布我的最后一次尝试。我将在大约十分钟后发布我的新尝试。希望我能弄明白。
  • Is this 你想要什么?或者至少它能让你走上正轨?
  • @Musefan 感谢您的帮助,您的答案实际上是我正在寻找的答案。如果您发布为答案,我将标记为答案并投票。

标签: javascript jquery css menu html-lists


【解决方案1】:

你可以:

  1. 在文档中循环所有 ul
  2. foreach 元素计数子项
  3. 如果发现 ul 元素下的 li 数 > 5
  4. 使用所需列表的 html 创建一个新列表

$(document).ready(function(){
    $('.submenu-wrapper').each(function(){
        var count_li=0; 
        var i=1;
        $(this).children('ul').children('li').each(function(){
            count_li++;
            if(count_li>5 && i==1){
               $(document.body).append('<ul id="newlist"></ul>');
               $('#newlist').append($(this).nextUntil($(this).last()).andSelf());
               i++;
            }
         });
        
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<ul id="nav" class="se test">
<li id="menu1" class="page-1307 parent-menu parent">
    <div class="nav-inner">
        <a class="menulink" id="menuitem1" onclick="return false" href="#">test<span class="toggle"></span></a>
        <ul id="ie1" class="plain">
            <li class="parent-menu parent"><a href="test">test<span class="toggle"></span></a>
                <div class="submenu-wrapper">
                    <ul class="plain">
                        <li><a href="test">test11</a></li>
                        <li><a href="test">test12</a></li>
                        <li><a href="test">test13</a></li>
                        <li><a href="test">test14</a></li>
                        <li><a href="test">test15</a></li>
                        <li><a href="test">test16</a></li>
                        <li><a href="test">test17</a></li>
                    </ul>
                </div>
           </li>
           <li class="parent-menu parent"><a href="test">test<span class="toggle"></span></a>
                <div class="submenu-wrapper">
                    <ul class="plain">
                        <li><a href="test">test21</a></li>
                        <li><a href="test">test22</a></li>
                        <li><a href="test">test23</a></li>
                        <li><a href="test">test24</a></li>
                    </ul>
               </div>
           </li>
        </ul>
    </div>
</li>
<li id="menu2" class="menulink page-7">
    <div class="nav-inner">
        <a href="test">test</a>
    </div>
</li>
</ul>

【讨论】:

  • 这真的很接近我想要做的,除了我需要在五个之后删除剩余的列表项并将剩余的列表项放入新的 ul 中。我会拿走你所拥有的,试着把它塑造成我需要的,谢谢。
【解决方案2】:

这是我一直在寻找的最终答案:

megaMenu: function(){
    function addNewList(current, newItems) {
        var newList = $('<li class="parent-menu parent newLi">');
        var div = $('<div class="submenu-wrapper">');
        newList.append(div);
        var ul = $('<ul class="plain">');
        div.append(ul);
        for (var i = 0; i < newItems.length; i++) {
            ul.append(newItems[i]);
        }
        current.after(newList);
        return newList;
    }
    function splitLists() {
        var allLists = $(".plain > li.parent-menu");
        for (var i = 0; i < allLists.length; i++) {
            var currentList = $(allLists[i]);
            var items = currentList.find("li");

            if (items.length > 5) {
                var temp = [];
                for (var j = 5; j < items.length; j++) {
                    temp.push($(items[j]));
                    if (temp.length == 5) {
                        currentList = addNewList(currentList, temp);
                        temp = [];
                    }
                }
                if (temp.length > 0) {
                    currentList = addNewList(currentList, temp);
                }
            }
        }
    }
    splitLists();
}

【讨论】:

  • 对于我在团队会议上的延迟回答,我深表歉意。再次感谢您的帮助和指导。
【解决方案3】:

在通过 cmets 进行了一些澄清之后,您似乎正在寻找类似的东西。我已经注释了代码来解释这个过程背后的逻辑:

// function for adding a new LI item.
function addNewList(current, newItems) {
    // Create the new li node.
    var newList = $('<li class="parent-menu parent">');

    // Add the initial a link.
    newList.append('<a href="test">test<span class="toggle"></span></a>');

    // Create and append the submenu-wrapper div to our new list item.
    var div = $('<div class="submenu-wrapper">');
    newList.append(div);

    // Create and append the new ul node to our submenu-wrapper div.
    var ul = $('<ul class="plain">');
    div.append(ul);

    // Loop the 5 (or less) items that have been specified and add them to our new list.
    for (var i = 0; i < newItems.length; i++) {
        // Using append will move the elements that already exist in the original place.
        ul.append(newItems[i]);
    }

    // Add our new list item to the DOM.
    current.after(newList);

    return newList;
}

// Base function to split the lists as required.
function splitLists() {

    // Get all the lists that we want to process.
    var allLists = $(".plain > li.parent-menu");

    // Loop each list and process.
    for (var i = 0; i < allLists.length; i++) {
        var currentList = $(allLists[i]);

        // Get the sub-items that we need to split.
        var items = currentList.find("li");

        // We only care about lists that are more than 5 items.
        if (items.length > 5) {
            // Create array to store the items that we want to move (any after first 5)
            var temp = [];
            // Start at the 6th item an start moving them in blocks of 5.
            for (var j = 5; j < items.length; j++) {
                // Add the item to move to our temp array.
                temp.push($(items[j]));

                // If we have 5 in our temp array then move them to new list.
                if (temp.length == 5) {
                    // Move items with helper function.
                    currentList = addNewList(currentList, temp);
                    // Clear the temp array ready for the next set of items.
                    temp = [];
                }
            }
            // If we have any spare ones that didn't get handle in the length == 5 check, then process them now.
            if (temp.length > 0) {
                currentList = addNewList(currentList, temp);
            }
        }
    }
}

// Run the process.
splitLists();

Here is a working example

【讨论】:

    猜你喜欢
    • 2017-04-03
    • 1970-01-01
    • 1970-01-01
    • 2013-10-02
    • 2014-06-25
    • 2019-01-24
    • 1970-01-01
    • 1970-01-01
    • 2017-12-11
    相关资源
    最近更新 更多