jquery: reverse an order

≯℡__Kan透↙ 提交于 2019-12-03 02:25:28

If you have a container around the list, it's a little easier:

$("#container").append($(".block-item").get().reverse());

http://jsfiddle.net/BhTEN/12/

You can use this:

$($(".block-item").get().reverse()).each(function (i) {
    $(this).text(++i);
});

Demo here.
Second demo here (changing the DOM elements positioning).

Another way, using also jQuery with reverse is:

$.fn.reverse = [].reverse;
$(".block-item").reverse().each(function (i) {
    $(this).text(++i);
});

This demo here.
Second demo here (changing the DOM elements positioning).

One more alternative is to use the length (count of elements matching that selector) and go down from there using the index of each iteration. Then you can use this:

var nr_of_divs = $(".block-item").length;
$(".block-item").each(function (i) {
    $(this).text(nr_of_divs - i);
});

This demo here
Second demo here (changing the DOM elements positioning).

One more, kind of related to the one above:

var nr_of_divs = $(".block-item").length;
$(".block-item").text(function (i) {
    return nr_of_divs - i;
});

Demo here

Your code is working. Just choose jQuery framework on the left hand.

 $($(".block-item").get().reverse()).each(function() {
     $(this).appendTo($(this).parent());
 });

Could this be what you are looking for?

http://plugins.jquery.com/project/reverseorder

try this:

function disp(divs) {
      var a = [];
      for (var i = 0; i < divs.length; i++) {
        a.push(divs[i].innerHTML);
      }
    alert(a);
    }

    disp( $("div.block-item").get().reverse() );

DEMO

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