本文翻译自:JQuery .each() backwards

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: 例如:

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


#1楼

参考:https://stackoom.com/question/5qeC/JQuery-each-向后


#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 here 在这里演示

... and with jQuery: ...和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: 另一种方式,使用反向 jQuery是:

$.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. 一种选择是使用length (匹配该选择器的元素的数量)并使用每次迭代的index从那里向下移动。 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 在这里演示


#6楼

我想你需要

.parentsUntill()

JQuery .each()向后相关推荐

  1. jquery.validator验证后ajax提交出现错误解决

    今天使用jquery.validator验证后作ajax提交,总是不能成功:之后发现原来是页面中有报错没有解决.当把页面中的报错解决掉时,可以ajax提交了 <!DOCTYPE html> ...

  2. JQuery获取append后的动态元素:live()和on()

    2019独角兽企业重金招聘Python工程师标准>>> jquery通过 live() 方法附加的事件处理程序适用于匹配选择器的当前及未来的元素(比如由脚本创建的新元素) $(&qu ...

  3. jQuery Ajax调用后如何管理重定向请求

    我使用$.post()使用Ajax调用servlet,然后使用生成的HTML片段替换用户当前页面中的div元素. 但是,如果会话超时,服务器将发送重定向指令以将用户发送到登录页面. 在这种情况下,jQ ...

  4. jquery网页刷新后控件失效_jquery动态增减控件如何才能不刷新页面

    已结贴√ 问题点数:20 回复次数:9 jquery动态增减控件如何才能不刷新页面 用jquery动态管理控件,可是每一次增减控件都会刷新页面,然后控件里面原来输入的值就都不在了,可以怎么来实现在动态 ...

  5. js 解析php serialize,php如何解析jquery serialize 提交后的数据

    客户端通过jquery serialize 提交表单数据 $("#submit_survey").click(function(){ $.post(SITE_URL+'activi ...

  6. ajax jinja,在向Flask发出jQuery AJAX请求后渲染Jinja

    我有一个Web应用程序,当HTML中的select元素发生更改时,它会从Flask获取动态数据.当然这是通过jquery ajax完成的.没有probs在这里我得到了. 问题是,Flask发送的动态数 ...

  7. jquery在节点后后添加html,jquery如何添加节点?

    jquery如何添加节点?下面本篇文章给大家介绍一下使用jquery添加节点的方法.有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助. jquery如何添加节点? jQuery插入节点的 ...

  8. jquery追加html后删除,jquery动态增加删除元素节点

    jquery动态增加删除元素节点 对于语言编程,不同的思路有着不同的解决办法,Jquery对于动态的ul-li节点的增加删除实力操作思路: 1.做一个按钮用于增加li节点,使用Jquery:appen ...

  9. Easyui笔记:jquery执行append后input的验证失效解决方案

    $('input[type!="hidden"],select,textarea',$("#表单ID")).each(function(){//执行验证器初始化 ...

  10. jQuery对象和DOM对象使用说明

    1.jQuery对象和DOM对象第一次学习jQuery,经常分辨不清哪些是jQuery对象,哪些是 DOM对象,因此需要重点了解jQuery对象和DOM对象以及它们之间的关系.DOM对象,即是我们用传 ...

最新文章

  1. VirtualBox32位系统上安装64位系统
  2. sublime text 使用笔记
  3. java远程操作ftp服务器上传下载
  4. mysql 001_Mysql错误积累001
  5. CCNP学习笔记2-路由部分--EIGRP
  6. linux64位ioremap函数,linux操作系统中的ioremap函数详解
  7. AM2320 温湿度计 单总线读取数据
  8. gprof, Valgrind and gperftools - an evaluation of some tools for application level CPU profiling on
  9. ethtool源码分析
  10. 二、2.4版本之前的apache的安装
  11. Asp.net MVC应用在IIS7上部署后403错误解决方案
  12. php js轮播图片代码,javascript实现焦点图轮播效果代码示例
  13. 关于代码运行速度与cpu关系的一点小事
  14. android音乐16bit,16bit音乐是无损吗
  15. React 全屏监听Esc键
  16. 苹果电脑切换任务管理器快捷键
  17. 【工具】60 个相见恨晚的神器工具
  18. (微软官方工具)局域网键鼠共享工具:Mouse without borders
  19. 日历显示为何成了107年2月18日
  20. 牢记公式,ardupilot EKF2就是纸老虎(一)!

热门文章

  1. 你真的会用java注解吗?
  2. 传统的Linux中IPC通信原理
  3. Android之linux基础教学之八 内核同步介绍
  4. 算法--------二叉树的中序遍历
  5. Android 平台的Crash崩溃捕获-全
  6. Apache MINA 2.0 用户指南
  7. wireshark tcp data中文_TCP的可靠传输
  8. (004)RN开发VSCode调试ReactNative项目
  9. Flutter开发之WebView加载网页(24)
  10. c++数据结构中 顺序队列的队首队尾_数据结构 3.3 顺序队