简体   繁体   English

当显示带有javascript的项目时,如何确保我的目录列表按字母顺序排序?

[英]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.

相关问题 如何创建目录列表,使用javascript将项目的每个首字母排序到一个字母组 - How can I create a Directory listing that sorts each first letter of a item to a letter-group with javascript jQuery Rotator按字母顺序排列,如何在我的主页上使其随机化? - jQuery Rotator Is Alphabetical, how can I make it randomized on my homepage? 如何确保我的 PHP api 只能由特定的 javascript 页面使用 - How can I make sure that my PHP api can only be used by a specific javascript page 如何确保通过CDN传送的JavaScript文件不会被更改? - How can I make sure that my JavaScript files delivered over a CDN are not altered? 我如何确保我的JavaScript在添加“…”之前删除空格。 - How can I make sure that my javascript removes whitespace before adding “…” 如何确保我的Javascript动画停留在其他固定元素之下? - How can I make sure my Javascript animation stay under other stationary elements? 对于我的Quicksort算法,我该如何使它对字符串和对象也进行排序? - For my Quicksort algorithm how can I make it so that it sorts Strings and objects as well? 如何确保在页面加载之前显示背景图像? - How can I make sure a background image displays before the page loads? 如何按字母顺序排列过滤器菜单 - How can I order the filter menu in alphabetical order 当新用户登录时,如何确保我的菜单刷新? 尝试显示正确的项目(管理员用户的管理员导航) - How do I make sure my menu refreshes when a new user logs in? Trying to show the proper items (admin navigation for admin users)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM