464

我正在使用 JQuery 选择页面上的一些元素,然后在 DOM 中移动它们。我遇到的问题是我需要以 JQuery 自然想要选择它们的相反顺序选择所有元素。例如:

<ul>
   <li>Item 1</li>
   <li>Item 2</li>
   <li>Item 3</li>
   <li>Item 4</li>
   <li>Item 5</li>
</ul>

我想选择所有 li 项目并在它们上使用 .each() 命令,但我想从项目 5 开始,然后是项目 4 等。这可能吗?

4

11 回答 11

716
$($("li").get().reverse()).each(function() { /* ... */ });
于 2009-09-08T13:31:07.933 回答
420

我以世界上最小的 jquery 插件的形式向您展示了最简洁的方式:

jQuery.fn.reverse = [].reverse;

用法:

$('jquery-selectors-go-here').reverse().each(function () {
    //business as usual goes here
});

-Michael Geary 在他的帖子中的所有功劳:http: //www.mail-archive.com/discuss@jquery.com/msg04261.html

于 2011-03-22T02:42:20.860 回答
64

你可以做

jQuery.fn.reverse = function() {
    return this.pushStack(this.get().reverse(), arguments);
}; 

其次是

$(selector).reverse().each(...) 
于 2009-09-08T13:34:06.213 回答
21

这里有不同的选项:

首先:没有jQuery:

var lis = document.querySelectorAll('ul > li');
var contents = [].map.call(lis, function (li) {
    return li.innerHTML;
}).reverse().forEach(function (content, i) {
    lis[i].innerHTML = content;
});

演示在这里

...并使用 jQuery:

你可以使用这个:

$($("ul > li").get().reverse()).each(function (i) {
    $(this).text( 'Item ' + (++i));
});

演示在这里

另一种方式,也使用 jQuery 与reverse是:

$.fn.reverse = [].reverse;
$("ul > li").reverse().each(function (i) {
    $(this).text( 'Item ' + (++i));
});

这个演示在这里

另一种选择是使用length(匹配该选择器的元素计数) 并使用index每次迭代的 the 向下移动。然后你可以使用这个:

var $li = $("ul > li");
$li.each(function (i) {
    $(this).text( 'Item ' + ($li.length - i));
});

这个演示在这里

还有一种,与上述有关:

var $li = $("ul > li");
$li.text(function (i) {
    return 'Item ' + ($li.length - i);
});

演示在这里

于 2013-08-19T18:22:56.443 回答
14

我更喜欢创建一个反向插件,例如

jQuery.fn.reverse = function(fn) {       
   var i = this.length;

   while(i--) {
       fn.call(this[i], i, this[i])
   }
};

用法例如:

$('#product-panel > div').reverse(function(i, e) {
    alert(i);
    alert(e);
});
于 2010-12-21T14:03:29.190 回答
9

如果您不想将方法保存到 jQuery.fn 中,您可以使用

[].reverse.call($('li'));
于 2017-03-31T08:09:02.127 回答
5

需要对 $.each 进行反向操作,所以我使用了 Vinay 的想法:

//jQuery.each(collection, callback) =>
$.each($(collection).get().reverse(), callback func() {});

工作得很好,谢谢

于 2010-06-17T04:00:31.533 回答
4

您不能使用 jQuery 每个函数向后迭代,但您仍然可以利用 jQuery 语法。

尝试以下操作:

//get an array of the matching DOM elements   
var liItems = $("ul#myUL li").get();

//iterate through this array in reverse order    
for(var i = liItems.length - 1; i >= 0; --i)
{
  //do Something
}
于 2009-09-08T13:30:22.070 回答
2

我发现Array.prototype.reverse对象不成功,所以我制作了一个新的 jQuery 函数作为替代方法:jQuery.eachBack(). 它像往常一样迭代jQuery.each(),并将每个键存储到一个数组中。然后它反转该数组并按照反转键的顺序对原始数组/对象执行回调。

jQuery.eachBack=function (obj, callback) {
    var revKeys=[]; $.each(obj,function(rind,rval){revKeys.push(rind);}); 
    revKeys.reverse();
    $.each(revKeys,function (kind,i){
        if(callback.call(obj[i], i, obj[i]) === false) {    return false;}
    });
    return obj;
}   
jQuery.fn.eachBack=function (callback,args) {
    return jQuery.eachBack(this, callback, args);
}
于 2015-12-09T01:14:17.970 回答
-2

我想你需要

.parentsUntill()
于 2014-02-14T08:00:56.930 回答
-2

你也可以试试

var arr = [].reverse.call($('li'))
arr.each(function(){ ... })
于 2017-07-28T12:38:30.213 回答