1

この質問を最初に投稿したときに間違えたため、この質問を再投稿しています。質問する前に十分な努力をしていませんでした。そして、私のために私の仕事をするようにコミュニティに依頼してくれたことをコミュニティに謝罪したいと思います

div内の順序付けされていないリストをアルファベット順に並べようとしています。順序付けされていないリスト内のリスト項目ではなく、順序付けされていないリスト自体です。

HTMLは次のようになります。

<div class="FindByCategory">
    <ul>
         <li> B. This is the first list item in the second unordered list</li>
         <li> B. This is the second list item in the second unordered list</li>
    </ul>
    <ul>
         <li> A. This is the first list item in the first unordered list</li>
         <li> A. This is the second list item in the first unordered list</li>
    </ul>
</div>

私はそれをこのように見せたいです:

<div class="FindByCategory">
    <ul>
         <li> A. This is the first list item in the first unordered list</li>
         <li> A. This is the second list item in the first unordered list</li>
    </ul>
    <ul>
         <li> B. This is the first list item in the second unordered list</li>
         <li> B. This is the second list item in the second unordered list</li>
    </ul>
</div>

これは私がこれまでに行ったことです:

    <script>
        function sortUnorderedList(div.FindByCategory, sortDescending) {
  if(typeof div.FindByCategory == "string")
    div = document.getElementById(div.FindByCategory);
  var uls = ul.getElementsByTagName("UL");
  var vals = [];
  for(var i = 0, l = uls.length; i < l; i++)
    vals.push(uls[i].innerHTML);
  vals.sort();
  for(var i = 0, l = uls.length; i < l; i++)
    uls[i].innerHTML = vals[i];
}
$(function(){
    FindByCategory()
    }
    </script>
4

3 に答える 3

1

これを試して

<script type="text/javascript">

                var array = new Array();

                for(var i = 0 ; i<4 ; i++){

                    var string = $("li:eq("+i+")").text();
                    array[i]  = string;

                }

                array.sort();

                alert(array);


            </script>
于 2012-06-20T17:50:18.880 に答える
1

DOM ノードをソートする場合、次の 3 つの簡単な手順があります。

  1. ソートするノードの配列を作成し、ソートする部分を指定します (すぐにアクセスできるプロパティでない場合)。
  2. 配列をソートします。
  3. 新しい順序に基づいて DOM ツリーを再構築します。

この場合、並べ替えたいノードは によって一致し#FindByCategory > ul、テキスト コンテンツによって効果的に並べ替えられます。そう:

var qsa = document.querySelectorAll("#FindByCateory > ul"), l = qsa.length, i, arr = [];
for( i=0; i<l; i++) {
    arr[i] = [qsa[i],qsa[i].textContent || qsa[i].innerText];
    // the text content is not readily accessible, since it depends on the browser
}
// now sort the array:
arr.sort(function(a,b) {return a[1] < b[1] ? -1 : (a[1] == b[1] ? 0 : 1);});
// and now re-append them
for( i=0; i<l; i++) {
    arr[i][0].parentNode.appendChild(arr[i][0]);
    // by re-appending the nodes, they will emerge sorted
}
于 2012-06-20T17:35:21.567 に答える
0

これは、jQuery 用のスイス アーミー ナイフ ソート プラグインへのリンクです。http://tinysort.sjeiti.com/

于 2012-06-20T17:26:41.797 に答える