gpt4 book ai didi

Javascript/JQuery 按字母顺序对带有 anchor 的列表进行排序

转载 作者:搜寻专家 更新时间:2023-11-01 04:46:42 25 4
gpt4 key购买 nike

我有这个列表:

<ul id="demoOne" class="demo">
<li><a href='http://site/Service.aspx?shId=1154'>Dhenie fotokopje aktesh nga dosja 12</a></li>
<li><a href='http://site/Service.aspx?shId=1155'>Depozitim kerkesash Ankimore/Rekurse
kunder vendimeve civile/penale 12</a></li>
<li><a href='http://site/Service.aspx?shId=1156'>Dhenie Vendimesh12</a></li>
<li><a href='http://site/Service.aspx?shId=1157'>Integrimi i Ish te Perndjekurve Polikite
12</a> </li>
<li><a href='http://site/Service.aspx?shId=1158'>Dhenie Drejtesie</a></li>
<li><a href='http://site/Service.aspx?shId=1159'>Gjykata e Rrethit Gjyqësor Lezhë ushtron
juridiksionin gjyqësor civil dhe penal në territorin e qarkut Lezhë të Republikës
së Shqipërisë.</a></li>
</ul>

我想按字母顺序排序这个列表,使用 anchor 文本,而不是 li 文本。如何做到这一点?

我正在使用此脚本按 li 文本对它们进行排序

function sortUnorderedList(ul, sortDescending) {
var items = $('.demo li').get();
items.sort(function(a,b){


var keyA = $(a).text();
var keyB = $(b).text();

if (keyA < keyB) return -1;
if (keyA > keyB) return 1;
return 0;

var keyA = $(a).text();
var keyB = $(b).text();

if (keyA < keyB) return -1;
if (keyA > keyB) return 1;
return 0;

});
var ul = $('.demo');
$.each(items, function(i, li){
ul.append(li);
});
}

最佳答案

试试这个:

$(function() {
$.fn.sortList = function() {
var mylist = $(this);
var listitems = $('li', mylist).get();
listitems.sort(function(a, b) {
var compA = $(a).text().toUpperCase();
var compB = $(b).text().toUpperCase();
return (compA < compB) ? -1 : 1;
});
$.each(listitems, function(i, itm) {
mylist.append(itm);
});
}

$("ul#demoOne").sortList();

});

关于Javascript/JQuery 按字母顺序对带有 anchor 的列表进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13394796/

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