jquery,将内部元素移动到第一个位置?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3506140/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 15:34:17  来源:igfitidea点击:

jquery, move an inner element to the first position?

jqueryhtmlsorting

提问by Andrew

Lets say you have a list of items:

假设您有一个项目列表:

<ul id="ImportantNumbers">
   <li id="one"></li>
   <li id="two"></li>
   <li id="topNumber"></li>
   <li id="four"></li>
</ul>

Every five seconds these list items get reordered.

每五秒,这些列表项就会重新排序。

Using jquerywhats the best way to keep #topNumber, at the top of the list during the reordering.

使用jquery最好的方法是#topNumber在重新排序期间将, 保持在列表的顶部。

回答by Nick Craver

You can use .prependTo()immediately after the re-order, like this:

您可以.prependTo()在重新订购后立即使用,如下所示:

$("#topNumber").prependTo("#ImportantNumbers");

You can see it working here, all it's doing is taking the element and inserting it as the first child on the <ul>, effectively moving it to the top.

你可以看到它在这里工作,它所做的就是获取元素并将它作为第一个子元素插入<ul>,有效地将它移动到顶部。

Alternatively when you sort, use :not()to exclude it depending on how the sorting works, for example:

或者,当您排序时,:not()根据排序的工作方式使用来排除它,例如:

$("#ImportantNumbers li:not(#topNumber)").randomize();