gpt4 book ai didi

javascript - 使用 jQuery 对 div 进行排序

转载 作者:太空狗 更新时间:2023-10-29 12:24:44 26 4
gpt4 key购买 nike

我正在尝试使用 jQuery sort div,它确实可以排序,但似乎排序不正确。 enter image description here

这是在应用 sort 之后

这里是 HTML 片段。

<div class="container column">
<div class="row">
<section class="col-md-10 col-md-offset-3">
<section class="col-md-2 col-md-offset-1"><a href="./stafferLink/index.php" >Staffer Link</a></section>
<section class="col-md-2 col-md-offset-1"><a href="./stafferLink/view.php">View Data</a></section>
</section>
</div>
</div>

jQuery

var $divs = $(".container.column");

$(document).ready(function () {
var alphabeticallyOrderedDivs = $divs.sort(function (a, b) {
return $(a).find("a:first").text() > $(b).find("a:first").text();
});
$("#container").html(alphabeticallyOrderedDivs);
});

PS:#container 是最外层的 div,它包含所有需要有序的 div

Here it's

抱歉,没有响应

最佳答案

这是一个大小写问题,请尝试对您的元素广告进行排序并忽略大小写并使用 localCompare 函数。

The localeCompare() method returns a number indicating whether a reference string comes before or after or is the same as the given string in sort order.

代码:

var alphabeticallyOrderedDivs = $divs.sort(function (a, b) {
return $(a).find("a:first").text().toLowerCase().localeCompare($(b).find("a:first").text().toLowerCase());
});

演示:http://jsfiddle.net/bk66on10/

引用:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare

关于javascript - 使用 jQuery 对 div 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31749051/

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