gpt4 book ai didi

javascript - 如何仅使用 Jquery 对 div 列表进行排序?

转载 作者:行者123 更新时间:2023-12-01 01:43:31 29 4
gpt4 key购买 nike

我有一个包含很多元素的 div 列表,我想做很多排序:

            <div id="lista-terremoti" style="">
<div class="eq-lista">
<div class="mark-eq-lista"><span style="display:none">1536017841000</span></div>
<div class="dati-eq">
<div class="eq-lista-row" style="">
<div class="eq-lista-data" style="">2018-09-03&nbsp;&nbsp;23:37:21 UTC</div>
<div class="eq-lista-luogo">Al largo dell'Ecuador</div>
<div id="magn-ipo-link">
<div class="eq-lista-magn">4.9 M</div>
<div class="eq-lista-ipo">40 Km</div>
<div class="eq-lista-link"><a href="#" rel="nofollow">Dettagli</a></div>
</div>
</div>
<div class="ulteriore" style="display:none"></div>
</div>
</div>
<div class="eq-lista">
<div class="mark-eq-lista"><span style="display:none">1536017747000</span></div>
<div class="dati-eq">
<div class="eq-lista-row" style="">
<div class="eq-lista-data" style="">2018-09-03&nbsp;&nbsp;23:35:47 UTC</div>
<div class="eq-lista-luogo">Isole Marianne</div>
<div id="magn-ipo-link">
<div class="eq-lista-magn">5 M</div>
<div class="eq-lista-ipo">200 Km</div>
<div class="eq-lista-link"><a href="#" rel="nofollow">Dettagli</a></div>
</div>
</div>
<div class="ulteriore" style="display:none"></div>
</div>
</div>
<div class="eq-lista">
<div class="mark-eq-lista"><span style="display:none">1536015211000</span></div>
<div class="dati-eq">
<div class="eq-lista-row" style="">
<div class="eq-lista-data" style="">2018-09-03&nbsp;&nbsp;22:53:31 UTC</div>
<div class="eq-lista-luogo">California centrale, Stati Uniti</div>
<div id="magn-ipo-link">
<div class="eq-lista-magn">2.5 M</div>
<div class="eq-lista-ipo">15 Km</div>
<div class="eq-lista-link"><a href="#" rel="nofollow">Dettagli</a></div>
</div>
</div>
<div class="ulteriore" style="display:none"></div>
</div>
</div>
<div class="eq-lista">
<div class="mark-eq-lista"><span style="display:none">1536015088000</span></div>
<div class="dati-eq">
<div class="eq-lista-row" style="">
<div class="eq-lista-data" style="">2018-09-03&nbsp;&nbsp;22:51:28 UTC</div>
<div class="eq-lista-luogo">Alaska meridionale</div>
<div id="magn-ipo-link">
<div class="eq-lista-magn">2.1 M</div>
<div class="eq-lista-ipo">74.6 Km</div>
<div class="eq-lista-link"><a href="#" rel="nofollow">Dettagli</a></div>
</div>
</div>
<div class="ulteriore" style="display:none"></div>
</div>
</div>
</div>

每个 div .eq-lista 都是一场包含许多数据的地震:UTC 日期(地震的时间戳位于 .mark-eq-lista > span 内) )、地点大小深度

我想以两种方式对列表进行排序:

1)从最旧到最新(与现在相反)排序

2) 按从最高到最低的幅度排序 (.eq-lista-magn)

我尝试了这段代码,但不起作用:

var items = $('#lista-terremoti > .eq-lista').get();
items.sort(function(a, b) {
var keyA = $(a).children(':first').children();
var keyB = $(a).children(':first').children();

if (keyA < keyB) return -1;
if (keyA > keyB) return 1;
return 0;
});
var div_list = $('#lista-terremoti');
$.each(items, function(i, quake) {
div_list.append(quake); /* This removes li from the old spot and moves it */
});

我希望你能帮助我......非常感谢!

最佳答案

我会将时间戳的获取和幅度的获取抽象为单独的函数,以保持代码干燥。然后,您可以按以下方式排序

items.sort((a, b) => (
eqToTimestamp(a) - eqToTimestamp(b)
|| eqToMagn(a) - eqToMagn(b)
));

请注意,时间戳比较通常会得出非 0 的结果,在这种情况下,大小差异很少会对列表顺序产生影响。

const eqToTimestamp = eq => Number(eq.children[0].textContent);
const eqToMagn = eq => (
Number(eq.querySelector('.eq-lista-magn').textContent.match(/\S+/))
);
const items = $('#lista-terremoti > .eq-lista').get();
items.sort((a, b) => (
eqToTimestamp(a) - eqToTimestamp(b)
|| eqToMagn(b) - eqToMagn(a)
));

const div_list = $('#lista-terremoti');
items.forEach(item => div_list.append(item));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="lista-terremoti" style="">
<div class="eq-lista">
<div class="mark-eq-lista"><span style="display:none">1536017841000</span></div>
<div class="dati-eq">
<div class="eq-lista-row" style="">
<div class="eq-lista-data" style="">2018-09-03&nbsp;&nbsp;23:37:21 UTC</div>
<div class="eq-lista-luogo">Al largo dell'Ecuador</div>
<div id="magn-ipo-link">
<div class="eq-lista-magn">4.9 M</div>
<div class="eq-lista-ipo">40 Km</div>
<div class="eq-lista-link"><a href="#" rel="nofollow">Dettagli</a></div>
</div>
</div>
<div class="ulteriore" style="display:none"></div>
</div>
</div>
<div class="eq-lista">
<div class="mark-eq-lista"><span style="display:none">1536017747000</span></div>
<div class="dati-eq">
<div class="eq-lista-row" style="">
<div class="eq-lista-data" style="">2018-09-03&nbsp;&nbsp;23:35:47 UTC</div>
<div class="eq-lista-luogo">Isole Marianne</div>
<div id="magn-ipo-link">
<div class="eq-lista-magn">5 M</div>
<div class="eq-lista-ipo">200 Km</div>
<div class="eq-lista-link"><a href="#" rel="nofollow">Dettagli</a></div>
</div>
</div>
<div class="ulteriore" style="display:none"></div>
</div>
</div>
<div class="eq-lista">
<div class="mark-eq-lista"><span style="display:none">1536015211000</span></div>
<div class="dati-eq">
<div class="eq-lista-row" style="">
<div class="eq-lista-data" style="">2018-09-03&nbsp;&nbsp;22:53:31 UTC</div>
<div class="eq-lista-luogo">California centrale, Stati Uniti</div>
<div id="magn-ipo-link">
<div class="eq-lista-magn">2.5 M</div>
<div class="eq-lista-ipo">15 Km</div>
<div class="eq-lista-link"><a href="#" rel="nofollow">Dettagli</a></div>
</div>
</div>
<div class="ulteriore" style="display:none"></div>
</div>
</div>
<div class="eq-lista">
<div class="mark-eq-lista"><span style="display:none">1536015088000</span></div>
<div class="dati-eq">
<div class="eq-lista-row" style="">
<div class="eq-lista-data" style="">2018-09-03&nbsp;&nbsp;22:51:28 UTC</div>
<div class="eq-lista-luogo">Alaska meridionale</div>
<div id="magn-ipo-link">
<div class="eq-lista-magn">2.1 M</div>
<div class="eq-lista-ipo">74.6 Km</div>
<div class="eq-lista-link"><a href="#" rel="nofollow">Dettagli</a></div>
</div>
</div>
<div class="ulteriore" style="display:none"></div>
</div>
</div>
</div>

要仅按幅度排序,请使用 items.sort((a, b) => eqToMagn(a) - eqToMagn(b));:

const eqToTimestamp = eq => Number(eq.children[0].textContent);
const eqToMagn = eq => (
Number(eq.querySelector('.eq-lista-magn').textContent.match(/\S+/))
);
const items = $('#lista-terremoti > .eq-lista').get();
items.sort((a, b) => eqToMagn(b) - eqToMagn(a));

const div_list = $('#lista-terremoti');
items.forEach(item => div_list.append(item));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="lista-terremoti" style="">
<div class="eq-lista">
<div class="mark-eq-lista"><span style="display:none">1536017841000</span></div>
<div class="dati-eq">
<div class="eq-lista-row" style="">
<div class="eq-lista-data" style="">2018-09-03&nbsp;&nbsp;23:37:21 UTC</div>
<div class="eq-lista-luogo">Al largo dell'Ecuador</div>
<div id="magn-ipo-link">
<div class="eq-lista-magn">4.9 M</div>
<div class="eq-lista-ipo">40 Km</div>
<div class="eq-lista-link"><a href="#" rel="nofollow">Dettagli</a></div>
</div>
</div>
<div class="ulteriore" style="display:none"></div>
</div>
</div>
<div class="eq-lista">
<div class="mark-eq-lista"><span style="display:none">1536017747000</span></div>
<div class="dati-eq">
<div class="eq-lista-row" style="">
<div class="eq-lista-data" style="">2018-09-03&nbsp;&nbsp;23:35:47 UTC</div>
<div class="eq-lista-luogo">Isole Marianne</div>
<div id="magn-ipo-link">
<div class="eq-lista-magn">5 M</div>
<div class="eq-lista-ipo">200 Km</div>
<div class="eq-lista-link"><a href="#" rel="nofollow">Dettagli</a></div>
</div>
</div>
<div class="ulteriore" style="display:none"></div>
</div>
</div>
<div class="eq-lista">
<div class="mark-eq-lista"><span style="display:none">1536015211000</span></div>
<div class="dati-eq">
<div class="eq-lista-row" style="">
<div class="eq-lista-data" style="">2018-09-03&nbsp;&nbsp;22:53:31 UTC</div>
<div class="eq-lista-luogo">California centrale, Stati Uniti</div>
<div id="magn-ipo-link">
<div class="eq-lista-magn">2.5 M</div>
<div class="eq-lista-ipo">15 Km</div>
<div class="eq-lista-link"><a href="#" rel="nofollow">Dettagli</a></div>
</div>
</div>
<div class="ulteriore" style="display:none"></div>
</div>
</div>
<div class="eq-lista">
<div class="mark-eq-lista"><span style="display:none">1536015088000</span></div>
<div class="dati-eq">
<div class="eq-lista-row" style="">
<div class="eq-lista-data" style="">2018-09-03&nbsp;&nbsp;22:51:28 UTC</div>
<div class="eq-lista-luogo">Alaska meridionale</div>
<div id="magn-ipo-link">
<div class="eq-lista-magn">2.1 M</div>
<div class="eq-lista-ipo">74.6 Km</div>
<div class="eq-lista-link"><a href="#" rel="nofollow">Dettagli</a></div>
</div>
</div>
<div class="ulteriore" style="display:none"></div>
</div>
</div>
</div>

关于javascript - 如何仅使用 Jquery 对 div 列表进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52157351/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com