gpt4 book ai didi

jquery - 使用 JQuery 对包含 HTML 内容的 HTML 列表进行排序

转载 作者:行者123 更新时间:2023-12-01 03:20:47 25 4
gpt4 key购买 nike

是否可以将下面的列表从较小的数字到较大的数字进行排序,同时保留每个 li 内容?

<ul>
<li>
39
<img class="botao-remover-tamanho" title="Remover cor" alt="" src="/admin/imagens/botao-excluir-cor.png">
<input type="hidden" title="39" value="39" name="array_variacao[]">
</li>
<li>
34
<img class="botao-remover-tamanho" title="Remover cor" alt="" src="/admin/imagens/botao-excluir-cor.png">
<input type="hidden" title="34" value="34" name="array_variacao[]">
</li>
<li>
38
<img class="botao-remover-tamanho" title="Remover cor" alt="" src="/admin/imagens/botao-excluir-cor.png">
<input type="hidden" title="38" value="38" name="array_variacao[]">
</li>
<li>
35
<img class="botao-remover-tamanho" title="Remover cor" alt="" src="/admin/imagens/botao-excluir-cor.png">
<input type="hidden" title="35" value="35" name="array_variacao[]">
</li>
<li>
33
<img class="botao-remover-tamanho" title="Remover cor" alt="" src="/admin/imagens/botao-excluir-cor.png">
<input type="hidden" title="33" value="33" name="array_variacao[]">
</li>
</ul>

我需要像下面这样自动排序,因为列表将是动态创建的。

<ul>
<li>
33
<img class="botao-remover-tamanho" title="Remover cor" alt="" src="/admin/imagens/botao-excluir-cor.png">
<input type="hidden" title="33" value="33" name="array_variacao[]">
</li>
<li>
34
<img class="botao-remover-tamanho" title="Remover cor" alt="" src="/admin/imagens/botao-excluir-cor.png">
<input type="hidden" title="34" value="34" name="array_variacao[]">
</li>
<li>
35
<img class="botao-remover-tamanho" title="Remover cor" alt="" src="/admin/imagens/botao-excluir-cor.png">
<input type="hidden" title="35" value="35" name="array_variacao[]">
</li>
<li>
38
<img class="botao-remover-tamanho" title="Remover cor" alt="" src="/admin/imagens/botao-excluir-cor.png">
<input type="hidden" title="38" value="38" name="array_variacao[]">
</li>
<li>
39
<img class="botao-remover-tamanho" title="Remover cor" alt="" src="/admin/imagens/botao-excluir-cor.png">
<input type="hidden" title="39" value="39" name="array_variacao[]">
</li>
</ul>

感谢您的帮助!

编辑:

根据评论中@Blender的建议,我使用了下面帖子中的代码,它对我来说效果很好。

What is the easiest way to order a <UL>/<OL> in jQuery?

最佳答案

尝试下面的代码,

$(function() {
$('button').click(function() {
var liContents = [];
$('ul li').each(function() {
liContents.push($(this).html());
});
liContents.sort(liSorter);
$('ul li').each(function() {
$(this).html(liContents.pop());
});
});
});

/*
Below function is kind of a workaround for the listed HTMl
you need to update it if you have proper HTML.
*/
function liSorter(a, b) {
return (parseInt(b) - parseInt(a));
}

DEMO

编辑:更新了您的标记以获得更好的代码,

  1. span 包裹这些号码标签如 <span class="num">39</span>
  2. 更新了排序器功能如下,

代码:

function numOrdDesc(a, b) {
var aTxt = parseInt($(a).find('.num').text(), 10);
var bTxt = parseInt($(b).find('.num').text(), 10);
return (bTxt - aTxt);
}

DEMO

关于jquery - 使用 JQuery 对包含 HTML 内容的 HTML 列表进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10288576/

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