0

みんな、おはよう、

私が持っているリストの直接のLIをソートするコードがいくつかありますが、サブレベルもアルファベット順にソートしたいのですが、クラスをサブレベルに追加するなど、試したことはすべて機能しません-これは追加するだけですこれは明らかに間違っています。

誰かがコードを見て、手を貸してくれませんか? とても感謝しています:)

http://codepen.io/andyjh07/pen/yetIq

4

1 に答える 1

1

これを試して:

function firstLevel(){
  var mylist = $('ul.sort');
  var listitems = mylist.children('li').get();

  listitems.sort(function(a, b) {
    var compA = $(a).text().toUpperCase();
    var compB = $(b).text().toUpperCase();
    return (compA < compB) ? -1 : (compA > compB) ? 1 : 0;
  });
  $.each(listitems, function(idx, itm) { 
    mylist.append(itm); 
  });

  secLevel();
}

function secLevel(){
  var sublist = $('ul.sort li ul');
  var sublistitems = sublist.children('li').get();
  sublistitems.sort(function(a, b) {
    var compA = $(a).text().toUpperCase();
    var compB = $(b).text().toUpperCase();
    return (compA < compB) ? -1 : (compA > compB) ? 1 : 0;
  });
  $.each(sublistitems, function(idx, itm2) { 
    sublist.append(itm2); 
  });

}


$(document).ready(function() {
  firstLevel()
});
于 2013-02-04T11:45:55.163 に答える