开发者

jquery, move an inner element to the first position?

开发者 https://www.devze.com 2023-01-11 19:31 出处:网络
Lets say yo开发者_如何学Gou have a list of items: <ul id=\"ImportantNumbers\"> <li id=\"one\"></li>

Lets say yo开发者_如何学Gou 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 jquery whats the best way to keep #topNumber, at the top of the list during the reordering.


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

$("#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.

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

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

精彩评论

暂无评论...
验证码 换一张
取 消