jQuery 如何在jquery中以相反的顺序迭代元素?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4496795/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 17:21:20  来源:igfitidea点击:

how to iterate elements in reverse order in jquery?

jqueryhtml

提问by illuminatus

i'm new to jquery.I'd like to know how to iterate form elements in reverse order in jquery using each()? Any help would be appreciated:)

我是 jquery 的新手。我想知道如何使用 each() 在 jquery 中以相反的顺序迭代表单元素?任何帮助,将不胜感激:)

回答by Bhanu Prakash Pandey

try this

尝试这个

$($("input").get().reverse()).each(function() { /* ... */ });

回答by Osiris

$($("input").get().reverse()).each(function() { 
   //function body here
});

The .get().reverse()returns the reversed array of all your elements You can then use each()to return each individual element.

.get().reverse()返回所有的元素的反阵列然后可以使用each()返回每个元素。

Hope this helps.

希望这可以帮助。

回答by Willem

I use a similar method as above, but shorter, define a jQuery reverse function to be the same as the reverse function of an array:

我使用与上面类似的方法,但更短,定义一个 jQuery 反向函数与数组的反向函数相同:

$.fn.reverse = [].reverse;

now you can use it like this:

现在你可以这样使用它:

$('img').reverse().each(function(){ /* do something */ });

回答by James Westgate

I prefer creating a reverse plug-in eg

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

jQuery.fn.reverse = function(fn) {

   var i = this.length;

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

Usage eg:

用法例如:

$('#product-panel > div').reverse(function(i, e) {
    alert(i);
    alert(e);
});

回答by qwertymk

Better yet

更好

$.each(  $( $('input').get().reverse() )  , function(){ /* ... */ });