简体   繁体   中英

Moving up list items with jquery

I want to move up single items of a list by clicking. I'm using jQuery but it does not work.

js fiddle

<ul>
    <li class="item">AAAAA</li>
    <li class="item">BBBBB</li>
    <li class="item">CCCCC</li>
    <li class="item">DDDDD</li>
</ul>
$(".item").click( function(){
    var item = $(this);
    item.insertBefore(item.before());
}); 

jsFiddle DEMO

Use prev() instead of before()

item.insertBefore(item.prev());

Try:

$(".item").click(function () {
    $(this).insertBefore($(this).prev());
});

jsFiddle example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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