博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JQuery .each()向后
阅读量:2290 次
发布时间:2019-05-09

本文共 2329 字,大约阅读时间需要 7 分钟。

本文翻译自:

I'm using JQuery to select some elements on a page and then move them around in the DOM. 我正在使用JQuery在页面上选择一些元素,然后在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. 我遇到的问题是我需要按照JQuery自然要选择它们的相反顺序选择所有元素。 For example: 例如:

  • Item 1
  • Item 2
  • Item 3
  • Item 4
  • Item 5

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? 我想选择所有的li项并对它们使用.each()命令,但是我想从第5项开始,然后是第4项等等。这可能吗?


#1楼

参考:


#2楼

You cannot iterate backwards with the jQuery each function, but you can still leverage jQuery syntax. 你不能使用jQuery向后迭代每个函数,但你仍然可以利用jQuery语法。

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}

#3楼

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

#4楼

You can do 你可以做

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

followed by 其次是

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

#5楼

Here are different options for this: 以下是不同的选项:

First : without jQuery: 第一 :没有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 演示

... and with jQuery: ...和jQuery:

You can use this: 你可以用这个:

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

Demo 演示

Another way, using also jQuery with is: 另一种方式,使用 jQuery是:

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

This demo . 这个演示 。

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. 一种选择是使用length (匹配该选择器的元素的数量)并使用每次迭代的index从那里向下移动。 Then you can use this: 然后你可以使用这个:

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

This demo 这个演示

One more , kind of related to the one above: 还有一种,与上述有关:

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

Demo 演示


#6楼

我想你需要

.parentsUntill()

转载地址:http://lvcnb.baihongyu.com/

你可能感兴趣的文章
hdu1045——Fire Net(二分图+行列匹配)
查看>>
hdu2444——The Accomodation of Students(判断二分图+匈牙利算法)
查看>>
hdu1083——Courses(匈牙利算法)
查看>>
poj2594——Treasure Exploration(闭包+最小路径覆盖)
查看>>
poj3020——Antenna Placement(最小边覆盖)
查看>>
hdu1151——Air Raid(最小路径覆盖)
查看>>
hdu1054——Strategic Game(最小顶点覆盖+邻接表)
查看>>
hdu3829——Cat VS Dog(最大独立集)
查看>>
九月总结——我的丑态(2016.10.9)
查看>>
hdu2255——奔小康赚大钱(二分图最优匹配+KM算法)
查看>>
hdu3488——Tour(有向环覆盖,二分图最佳匹配)
查看>>
poj3177——Redundant Paths(双连通分量)
查看>>
poj3281——Dining(网络流+拆点)
查看>>
poj1087——A Plug for UNIX(网络流,超级源点、汇点)
查看>>
poj2195——Going Home(最小费用最大流)
查看>>
poj2516——Minimum Cost(最小费用最大流)
查看>>
hdu4280——Island Transport(最大流SAP算法)
查看>>
hdu4292——Food(SAP+拆点)
查看>>
hdu4289——Control(最大流最小割+SAP)
查看>>
hdu3605——Escape(二分图多重匹配)
查看>>