[英]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)菜单 1有class="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 个菜单,但是当浏览器窗口很小时(主要是当网站显示到智能手机中时),第二个菜单不会显示(没关系),我需要将
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然后,通过$(“#菜单语言>礼”),我检索包含的菜单项数组中的所有
Now if te actual width is less than 986 px I have to put the element现在,如果实际宽度小于 986 像素,我必须放置元素
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
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-hidden
或mobile-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.