How to move lists

只谈情不闲聊 提交于 2019-12-20 07:51:11

问题


I have something like this:

<ul>
<li id="li1">1</li>
<li id="li2">2</li>
<li id="li3">3</li>
</ul>

And I wonder if there is possible to move the list number 3, to the place of the list number 1 using javascript or jquery, like this:

<ul>
<li id="li3">3</li>
<li id="li2">2</li>
<li id="li1">1</li>
</ul>

Thanks for you time!


回答1:


No jQuery solution :

var list = document.getElementsByTagName('ul')[0],
    items = list.getElementsByTagName('li'),
    i = items.length;
while (i--) list.appendChild(items[i]);

Here is a demo : http://jsfiddle.net/wared/tJaJ9/.


Based on cookie monster's suggestion :

var list = document.getElementsByTagName('ul')[0],
    i = list.children.length;
while (i--) list.appendChild(list.children[i]);

Just for fun :

var list = document.getElementsByTagName('ul')[0],
    items = Array.prototype.slice.call(list.children);
while (items.length) list.appendChild(items.pop());

A jQuery one :

$('ul').append($('li').get().reverse());



回答2:


You can use ajax sortable jquery plugin. One of my recommendation tutorial is Sortable Lists Using jQuery UI . Here user can re-order list using cursor pointer.




回答3:


This should do the trick for you.

var length = $('ul li').length;

while (length--) $('ul').append($('ul li')[length]);

Here is a working jsfiddle



来源:https://stackoverflow.com/questions/21589825/how-to-move-lists

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!