[英]How can I make sure that my directory listing sorts in alphabetical order when it displays the items with javascript?
I have a javascript that should displays items like this image: 我有一个javascript,它应该显示如下图所示的项目:
But it is not generating them in a alphabetical order like the image. 但这并不是像图像那样按字母顺序生成它们。
Here is a example how it is(only HTML & CSS included, its shows basicly how its rendered after the javascript has been executed): 这是一个示例(仅包含HTML和CSS,基本上显示了在执行javascript之后其呈现方式):
Take a look at: http://jsbin.com/UHiZUJU/20/edit 看看: http : //jsbin.com/UHiZUJU/20/edit
The javascript that generates these items with its Big letter is following: 生成这些带有大写字母的项目的JavaScript如下:
$(".processlinks-section-item-title2 a").each(function() {
var text = $(this).text();
var href = $(this).attr('href');
if (!text.length) return;
var letter = text.substr(0, 1).toUpperCase();
if (letter.match(/[0-9]/g))
letter = "#";
var list = $('.processlinks-section-template');
var section = list.children('[data-letter="'+letter+'"]');
if (!section.length) {
var section = $('<div class="processlinks-section-item" data-letter="' + letter + '"></div>');
if (!list.children().length) {
list.append(section);
} else {
// find right element so list is sorted
section.insertAfter(list.children()[0]);
}
}
var item = $('<div class="processlinks-section-item-title">' + '<a href="' + href + '">' + text + '</a>');
if (!section.children().length) {
section.append(item);
} else {
// find right element so list is sorted
item.insertAfter(section.children()[0]);
}
});
So my question is how can I sort these in alphabetical order? 所以我的问题是如何按字母顺序对它们进行排序?
Here is a testing JSbin where CSS,HTML and javascript is included but the javascript executes on button click, its just for testing. 这是一个测试JSbin,其中包含CSS,HTML和javascript,但是javascript单击按钮即可执行,仅用于测试。
Take a look at: http://jsbin.com/aMElAba/5/edit 看看: http : //jsbin.com/aMElAba/5/edit
Sort the items before using each
: 在使用
each
项目之前对项目进行排序:
$(".processlinks-section-item-title2 a").sort(function(a, b) {
var letterA = $(a).text().charAt(0).toUpperCase(),
letterB = $(b).text().charAt(0).toUpperCase();
return letterA > letterB ? -1 : (letterA < letterB ? 1 : 0);
}).each(...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.