JQuery .each() 向后

发布于 2024-08-04 08:16:41 字数 354 浏览 3 评论 0原文

我使用 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 等。这可能吗?

I'm using JQuery to select some elements on a page and then move them around in the DOM. The problem I'm having is I need to select all the elements in the reverse order that JQuery naturally wants to select them. For example:

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

I want to select all the li items and use the .each() command on them but I want to start with Item 5, then Item 4 etc. Is this possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

方圜几里 2024-08-11 08:16:41
$($("li").get().reverse()).each(function() { /* ... */ });
$($("li").get().reverse()).each(function() { /* ... */ });
时光倒影 2024-08-11 08:16:41

我以世界上最小的 jquery 插件的形式向您展示有史以来最干净的方式:

jQuery.fn.reverse = [].reverse;

用法:

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

-所有功劳均归功于 Michael Geary 在此处的帖子: http://www.mail-archive.com/[电子邮件受保护]/msg04261.html

I present you with the cleanest way ever, in the form of the world's smallest jquery plugin:

jQuery.fn.reverse = [].reverse;

Usage:

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

-All credit to Michael Geary in his post here: http://www.mail-archive.com/[email protected]/msg04261.html

萌酱 2024-08-11 08:16:41

您可以执行

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

以下操作

$(selector).reverse().each(...) 

You can do

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

followed by

$(selector).reverse().each(...) 
誰ツ都不明白 2024-08-11 08:16:41

这里有不同的选项:

第一个:不使用 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 与 反向 是:

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

此演示此处

另一种替代方案是使用length(与选择器匹配的元素计数),并使用每次迭代的index从那里向下。然后你可以使用这个:

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

这个演示这里

还有一个< /strong>,与上面的有点相关:

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

演示此处

Here are different options for this:

First: without 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;
});

Demo here

... and with jQuery:

You can use this:

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

Demo here

Another way, using also jQuery with reverse is:

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

This demo here.

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 $li = $("ul > li");
$li.each(function (i) {
    $(this).text( 'Item ' + ($li.length - i));
});

This demo here

One more, kind of related to the one above:

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

Demo here

っ左 2024-08-11 08:16:41

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

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);
});

I prefer creating a reverse plug-in eg

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

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

Usage eg:

$('#product-panel > div').reverse(function(i, e) {
    alert(i);
    alert(e);
});
花之痕靓丽 2024-08-11 08:16:41

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

[].reverse.call($('li'));

If you don't want to save method into jQuery.fn you can use

[].reverse.call($('li'));
清旖 2024-08-11 08:16:41

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

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

效果很好,谢谢

Needed to do a reverse on $.each so i used Vinay idea:

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

worked nicely, thanks

冷︶言冷语的世界 2024-08-11 08:16:41

您无法使用 jQuery every 函数向后迭代,但您仍然可以利用 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
}

You cannot iterate backwards with the jQuery each function, but you can still leverage jQuery syntax.

Try the following:

//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
}
未蓝澄海的烟 2024-08-11 08:16:41

我发现 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);
}

I found Array.prototype.reverse unsuccessful with objects, so I made a new jQuery function to use as an alternative: jQuery.eachBack(). It iterates through as the normal jQuery.each() would, and stores each key into an array. It then reverses that array and performs the callback on the original array/object in the order of the reversed keys.

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);
}
2024-08-11 08:16:41

这个答案类似于JoeChung的答案,不同之处在于通过这段代码你可以只使用容器元素(ul):

$($("ul").children().get().reverse()).each(function(i, elm) {

    console.log($(elm).text()) 

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul>
   <li>Item 1</li>
   <li>Item 2</li>
   <li>Item 3</li>
   <li>Item 4</li>
   <li>Item 5</li>
</ul>

This answer is like JoeChung's answer with the difference that by this code you can just use the container element (ul):

$($("ul").children().get().reverse()).each(function(i, elm) {

    console.log($(elm).text()) 

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul>
   <li>Item 1</li>
   <li>Item 2</li>
   <li>Item 3</li>
   <li>Item 4</li>
   <li>Item 5</li>
</ul>

权谋诡计 2024-08-11 08:16:41

你也可以尝试

var arr = [].reverse.call($('li'))
arr.each(function(){ ... })

You can also try

var arr = [].reverse.call($('li'))
arr.each(function(){ ... })
花想c 2024-08-11 08:16:41

我想你需要

.parentsUntill()

I think u need

.parentsUntill()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文