简体   繁体   English

如何把检索到的<li>使用 JQuery 的另一个无序列表末尾的无序列表元素?

[英]How to put the retrieved <li> element of an unordered list at the end of another unordered list using JQuery?

I am pretty new in JQuery and I have the following problem.我在 JQuery 中很新,我有以下问题。

Into a page I have 2 menus implemented by 2 differents unordered list , something like these:进入一个页面,我有 2 个菜单由 2 个不同的unordered list 实现,如下所示:

1) MENU 1 having class="nav single-page-nav" : 1)菜单 1class="nav single-page-nav"

<ul class="nav single-page-nav">
    <li>
        <a href="http://localhost/onyx/#section-45">Chi siamo</a>
    </li>
    <li>
        <a href="http://localhost/onyx/#section-27">Servizi</a>
    </li>
    <li>
        <a href="http://localhost/onyx/#section-174">Aggiornamenti</a>
    </li>
    <li>
        <a href="http://localhost/onyx/#section-121">Contattaci</a>
    </li>
</ul>

2) MENU 2 having class=nav 2)菜单 2有 class=nav

<ul id="menu-language" class="nav">
    <li id="menu-item-422" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-422">
        <a href="http://localhost/onyx/prova/">prova cat</a>
    </li>

    <li id="menu-item-185" class="qtranxs-lang-menu qtranxs-lang-menu-it menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-185">
        ..............................
        SOMETHING
        ..............................
    </li>
</ul>

So, for some reason in my page I need the 2 menus, but when the browser window is small (mainly when the website is showed into a smartphone) the second menu is not displayed (and it is ok) and I need to put the因此,出于某种原因,在我的页面中,我需要 2 个菜单,但是当浏览器窗口很小时(主要是当网站显示到智能手机中时),第二个菜单不会显示(没关系),我需要将

  • element of the second menu into the first one (after its element).第二个菜单的元素进入第一个菜单(在它的元素之后)。

    So I have done something like this:所以我做了这样的事情:

     jQuery(document).ready(function($){ ............................................ ............................................ ............................................ $(window).resize(function() { //alert("INTO resize") windowWidthResized = $(window).width(); //alert(windowWidthResized); console.log("WINDOWS WIDTH: " + windowWidthResized); menuItems = new Array(); menuItems = $('#menu-language > li'); console.log("NUMBER OF ITEMS IN SECOND MENU: " + menuItems.length); /* $('#menu-language > li').each(function() { console.log($(this).html()); //alert($(this).html()); }); */ if(windowWidthResized <= 986) { console.log("INSERT MENU 2 ITEMS AT THE END OF MENU 1"); } }); });

    So by the resize() method I intercept when the browser window change size and so I obtain the actual window width.因此,通过resize()方法,我在浏览器窗口更改大小时进行拦截,从而获得实际的窗口宽度。

    Then, by $('#menu-language > li') , I retrieve the menuItems array containing all the然后,通过$(“#菜单语言>礼”),我检索包含的菜单项数组中的所有

  • element (and the HTML content of these element) of the second menu.第二个菜单的元素(以及这些元素的 HTML 内容)。

    Now if te actual width is less than 986 px I have to put the element现在,如果实际宽度小于 986 像素,我必须放置元素

  • element retrieved as element of the menuItems array at the end of the first menu (the作为第一个菜单末尾的menuItems数组元素检索的元素( list having class="nav single-page-nav" )列表具有class="nav single-page-nav" )

    How can I do this last operation?我怎样才能做这最后的操作?

  • Try utilizing .appendTo尝试使用.appendTo

    $(window).resize(function() {
        //alert("INTO resize")
    
        windowWidthResized = $(window).width();
        //alert(windowWidthResized);
        console.log("WINDOWS WIDTH: " + windowWidthResized);
        var menuItems = $("#menu-language > li");
        console.log("NUMBER OF ITEMS IN SECOND MENU: " + menuItems.length);
    
        if(windowWidthResized <= 986) {
            console.log("INSERT MENU 2 ITEMS AT THE END OF MENU 1");
            menuItems.appendTo(".single-page-nav")
        } else {
            // if `li` within `#menu-language` already appended to
            // `.single-page-nav` append `#menu-language li`
            if ($("#menu-language > li").length === 0) {
              $(".single-page-nav li:gt(3)").appendTo("#menu-language")
            }
        }
    });
    

    这应该有效:

    $(".single-page-nav").append($("#menu-language").html());
    

    A short method:一个简短的方法:

    $("#menu-language").children().detach().//detach the LIs
    appendTo("ul.single-page-nav"); //append
    

    A Demo演示

    So, your code becomes:所以,你的代码变成:

    $(window).resize(function() {
    
        if($(window).width() <= 986) {
            if ($("#menu-language > li").length) {
                $("#menu-language > li").detach().appendTo("ul.single-page-nav");
            }
        //If you want to reset when the width is more than 986
        } else {
            $("ul.single-page-nav > li").slice(-2).appendTo("#menu-language");
        }
    });
    

    I would not recommend you to copy items with javascript.我不建议您使用 javascript 复制项目。 Why don't you use CSS Media Queries and hide subitems like with a class mobile-hidden or mobile-visible ?为什么不使用 CSS Media Queries 并隐藏子项,例如使用mobile-hiddenmobile-visible

    But to answer your question just created a demo here .但是为了回答您的问题,刚刚在此处创建了一个演示。 Use the two buttons to demonstrate the resize.使用两个按钮来演示调整大小。

    First of all add an id to your menu 1, I've used menu-1 , then use these lines of code to copy Elements from menu-language to menu-1 :首先向您的菜单 1 添加一个id ,我使用了menu-1 ,然后使用这些代码行将 Elements 从menu-language复制到menu-1

    var $menuItems = $('#menu-language > li');
    $menuItems.addClass('js-menu-1').appendTo('#menu-1');
    

    and these one to copy them back:这些是将它们复制回来的:

    var $menuItems = $('#menu-1 > li.js-menu-1');
    $menuItems.removeClass('js-menu-1').appendTo('#menu-language');
    

    暂无
    暂无

    声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

    相关问题 jQuery:如何确定索引 <li> 单击后将其置于无序列表中? - jQuery: How to determine the index of <li> element in an unordered list when clicked? 如何使用JQuery检索无序列表的li标签并将这些标签放入数组中? - How can I retrieve the li tag of an unordered list and put these tags into an array using JQuery? 如何使用jquery将无序列表附加到现有的html元素 - How to append an unordered list to en exisiting html element using jquery 如何使用jQuery排除无序列表中的最后一个元素 - How to exclude the last element in an unordered list using jQuery 具体造型 <li> 无序列表中的元素 <ul> - Styling specific <li> element in unordered list <ul> 如何在循环期间隐藏和检查无序列表中每个li的另一个元素的属性? - How do I hide and check the attribute of another element for each li in an unordered list during a loop? 如何使用jQuery显示和隐藏无序列表(ul)内的列表项(li)元素(最好)? - How to show and hide list items(li) elements inside unordered list(ul) using jquery(preferably)? 使用JQuery将列表元素从一个无序列表移动到另一个 - Moving list element from one unordered list to another with JQuery 如何使用jQuery创建无序列表 - How to create an unordered list using jQuery 尝试使用jQuery选择一个无序列表中的列表项和另一个无序列表 - Trying to select a list item within an unordered list with another unordered list using jquery
     
    粤ICP备18138465号  © 2020-2024 STACKOOM.COM