简体   繁体   English

如何在其CSS类的基础上将现有ul的所有li添加到另一个ul的底部

[英]How to add all li of existing ul to the bottom of another ul base on their css classes

I'm making a site responsive and I need to merge two different menus. 我正在使网站响应,并且我需要合并两个不同的菜单。 To do so I need to add all li of an ul to a different ul. 为此,我需要将ul的所有li添加到其他ul中。

Here is a simplification of current markup I have: 这是我目前的标记的简化:

<ul class="navigation-menu">
  <li>...</li>
  <li>...</li>
  <li>...</li>
</ul>

<ul class="nav-bar">
  <li>...</li>
  <li>...</li>
  <li>...</li>
</ul>

I need to add all li of nav-bar to the bottom of the list of ul with class navigation-menu and then prevent nav-bar from being displayed in the browser. 我需要使用类Navigation-menu将nav-bar的所有li添加到ul列表的底部,然后阻止nav-bar在浏览器中显示。

Here is my attempt: 这是我的尝试:

$('ul.nav-bar li').each(function() {
   $(this).after('ul.nav-bar:last-child');
}

Not sure how to tackle this one. 不确定如何解决这一问题。 I'll appreciate if some one can point me in the right direction. 如果有人能指出正确的方向,我将不胜感激。

you can use appendTo() which will add the items to the bottom of .navigation-menu : 您可以使用appendTo()将项目添加到.navigation-menu的底部:

$('ul.nav-bar li').each(function() {
  $(this).appendTo('ul.navigation-menu');
});

And you can use media queries to hide the other div at a certain screen width (although it will be "hidden" because it will be empty): 而且,您可以使用媒体查询以特定的屏幕宽度隐藏另一个div(尽管由于它为空,所以将被“隐藏”):

@media only screen and (max-width: 500px //example){
  .nav-bar{
    display: none;
  }
}

or JS if you prefer, but no reason to use JS when you can use CSS: 或JS(如果愿意),但是在可以使用CSS的情况下没有理由使用JS:

$('ul.nav-bar').hide();

FIDDLE 小提琴

Update 更新资料

As vladkras pointed out you can shorten the JS to simply be: 正如vladkras指出的,您可以将JS简化为:

$('ul.nav-bar li').appendTo('ul.navigation-menu');

NEW FIDDLE 新产品

Hello here is example Example 你好这里是例子

All you need is to clone list elements and append them after existing second menu :) 您所需要做的就是克隆列表元素,并将其附加在现有的第二个菜单之后:)

 $(function(){ var children = $(".nav-bar li").clone(); $(".navigation-menu").append(children); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul class="navigation-menu"> <li>1</li> <li>2</li> <li>3</li> </ul> <ul class="nav-bar"> <li>4</li> <li>5</li> <li>6</li> </ul> 

And hide element for example on breakpoint 769px 并例如在断点769px上隐藏元素

@media screen and (max-width:769px){
    .nav-bar{
        display: none;
    }
}
var navBar = $('.nav-bar'),
    LIs = navBar.children('li');
$('.navigation-menu').append(LIs);
navBar.hide(); // or .remove() if you don't need it anymore

FIDDLE 小提琴

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM