jquery:反转顺序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6219152/
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
jquery: reverse an order
提问by laukok
How can I reverse an order with jquery?
如何使用 jquery 反转订单?
I tried with the suggestion like this but it won't work!
我尝试过这样的建议,但它不起作用!
$($(".block-item").get().reverse()).each(function() { /* ... */ });
Have a look here.
看看这里。
I want the boxed to be rearranged like this,
我希望盒装可以像这样重新排列,
18
17
16
etc
Thanks.
谢谢。
回答by Jamie Treworgy
If you have a container around the list, it's a little easier:
如果列表中有一个容器,那就更容易了:
$("#container").append($(".block-item").get().reverse());
$("#container").append($(".block-item").get().reverse());
回答by Sergio
You can use this:
你可以使用这个:
$($(".block-item").get().reverse()).each(function (i) {
$(this).text(++i);
});
Demo here.
Second demo here(changing the DOM elements positioning).
Anotherway, using also jQuery with reverseis:
另一种方式,也使用 jQuery反向是:
$.fn.reverse = [].reverse;
$(".block-item").reverse().each(function (i) {
$(this).text(++i);
});
This demo here.
Second demo here(changing the DOM elements positioning).
这个演示在这里。此处的
第二个演示(更改 DOM 元素定位)。
One morealternative 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:
另一种选择是使用length
(与该选择器匹配的元素计数)并使用index
每次迭代的 。然后你可以使用这个:
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
演示在这里
回答by Farshid Zaker
Your code is working. Just choose jQuery framework on the left hand.
您的代码正在运行。只需选择左侧的 jQuery 框架即可。
$($(".block-item").get().reverse()).each(function() {
$(this).appendTo($(this).parent());
});