基于属性'data-sort'对Jquery中的Div进行排序? [英] Sort Divs in Jquery Based on Attribute 'data-sort'?
本文介绍了基于属性'data-sort'对Jquery中的Div进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我有几个div:
<div data-sort='1'>div1</div>
<div data-sort='4'>div4</div>
<div data-sort='8'>div8</div>
<div data-sort='12'>div12</div>
<div data-sort='19'>div19</div>
我动态创建div:
<div data-sort='14'>div1</div>
<div data-sort='6'>div1</div>
<div data-sort='9'>div1</div>
我怎样才能让他们按顺序排序已经加载的div,而不必重新加载所有divs?
How can I get them to just sort into the divs already loaded in order, without having to reload all of the divs?
我认为我需要在屏幕上构建所有div的数据排序值数组,然后看看新的位置divs适合,但我不确定这是否是最佳方式。
I think that I would need to build an array of the data-sort values of all of the divs on the screen, and then see where the new divs fit in,, but I am not sure if this is the best way.
感谢任何帮助
推荐答案
使用此功能
$('div').sort(function (a, b) {
var contentA =parseInt( $(a).attr('data-sort'));
var contentB =parseInt( $(b).attr('data-sort'));
return (contentA < contentB) ? -1 : (contentA > contentB) ? 1 : 0;
});
您可以在添加新div后立即调用此函数
you can call this function just after adding new divs
这篇关于基于属性'data-sort'对Jquery中的Div进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文