jQuery在div列表中的正确位置插入div [英] jQuery insert div at right place in list of divs
本文介绍了jQuery在div列表中的正确位置插入div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数十个DIV的列表,如
I have a list of dozens DIVs like
<div data-sort="7.5"></div>
<div data-sort="3.2"></div>
<div data-sort="2.15"></div>
<div data-sort="-1.78"></div>
<div data-sort="-2.39"></div>
等等,我得到一个带有ajax请求的新DIV。我需要插入新的DIV,所以使用数据排序的顺序依次保持不变。
and so on and I get new DIVs with an ajax request. I need to insert the new DIV so the order with "data-sort" remains intact in descending order.
推荐答案
它不是最优可以,但是我每次ajax请求完成时都会排序div:
It's not as optimal as can be, but I would just sort the divs each time the ajax request completes:
$("div").sort(function (prev, next) {
return parseInt(next.dataset.sort) - parseInt(prev.dataset.sort);
}).appendTo("body");
如果需要太长时间,您只需要遍历div(通过 .each
?),并找到第一个 data-sort
较小的
If that takes too long, you'll just have to cycle through the divs (via .each
?) and find the first one whose data-sort
is smaller.
这篇关于jQuery在div列表中的正确位置插入div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文