JQuery中元素的排序列表 [英] Sorting list of elements in JQuery

查看:113
本文介绍了JQuery中元素的排序列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用JQuery获得的元素列表.

I have a list of elements that I get using JQuery.

var $self = $(this);
var $fields = $('.identifier-controls', $self);

此列表是控件呈现时需要以某种方式进行操作的元素的列表. $ fields列表中的每个元素都包含一个称为"data-order"的数据属性.此属性告诉我在控件中应按什么顺序排列元素(ah要求).该顺序不必是直接线性顺序(这意味着第一个控件的属性值可以为10,下一个控件的属性值可以为15,下一个17的属性值等等).它们只需要按asc顺序出现即可.实现这一目标的方法?我能想到的所有方法似乎都有些复杂.

This list is the list of elements that need to be manipulated in some way when the control renders. Each element in the list of $fields contains a data attribute called "data-order". This attribute tells me what order I should arrange the elements with in the control (ah requirements). The order does not have to be in direct linear order (meaning that the first control can have an attribute value of 10 and the next one 15 and the next one 17 etc. They just need to appear in the asc order. Is there a simple way to achieve this? All the ways that I can come up with seem a bit over complicated.

推荐答案

尝试一下:

$(function(){
   var $self = $(this);
   var sortedList = $('.identifier-controls', $self).sort(function(lhs, rhs){
      return parseInt($(lhs).attr("data-order"),10) - parseInt($(rhs).attr("data-order"),10);
   });
});

变量sortedList现在具有已排序的元素.

the variable sortedList now has the sorted elements.

这篇关于JQuery中元素的排序列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆