0

現在、Squarespace.com プラットフォームで作業しており、デフォルトでアルファベット順に並べ替えられる動的なブログ カテゴリ インデックスを再並べ替えしたいと考えています。これは、Squarespace がコードを生成する方法です。

<div id="moduleContentWrapper11663355" class="widget-wrapper widget-type-journalarchive">
    <div id="moduleContent11663355">
        <ul class="archive-item-list-pt">
            <li>
            <a href="/blog/category/category-one">Category One</a>
            <span class="entry-count">(1)</span>
            </li>
            <li>
            <a href="/blog/category/category-two">Category Two</a>
            <span class="entry-count">(1)</span>
            </li>
            <li>
            <a href="/blog/category/category-three">Category Three</a>
            <span class="entry-count">(1)</span>
            </li>
        </ul>
    </div>
</div>

Jquery を使用してエントリを再ソートすることはできますか、または最悪の場合、CSS<li>を使用してそれぞれを操作できるように、それぞれに一意の CSS クラスを追加できますか?

ありがとう!

4

5 に答える 5

4

dom は実際には複雑なコレクションに他なりません... jquery を使用して li を配列のように並べ替えることができます。

var mylist = $('#moduleContent11663355 > ul');
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); });

http://www.onemoretake.com/2009/02/25/sorting-elements-with-jquery/に感謝

于 2011-08-22T17:29:43.380 に答える
3
var i = 1;
$('.archive-item-list-pt li').each(function(item) {
   $(item).addClass('myClass'+i);
});
于 2011-08-22T17:26:30.417 に答える
2
$('.archive-item-list-pt li').each(function(i,j) {
   $(this).addClass('yourClass'+i);
});

ここにフィドルがありますhttp://jsfiddle.net/h2gfT/7/

アップデート

$('.archive-item-list-pt li').each(function(i,j) {
   var cls=$(this).children("a").text().toLowerCase();
    var idx = cls.replace(' ','-');

    $(this).addClass(idx);
});

ここにフィドルがありますhttp://jsfiddle.net/h2gfT/9/

jquery チェーンのホットネスを持つもの

$('.archive-item-list-pt li').each(function(i,j) {
   var cls=$(this).children("a").text().toLowerCase().replace(' ','-');  
   $(this).addClass(cls);
});

http://jsfiddle.net/h2gfT/11/

于 2011-08-22T17:40:35.047 に答える
0

任意のキー関数に基づいてアイテムを並べ替える

http://blog.mirotin.net/125/jquery-sortitems

于 2011-08-23T09:30:05.530 に答える
-1

Sorting the entries with jQuery can be done in a couple diffrent ways, but if you just want to append CSS to each of them you could do that in two different ways.

$('li').each(function(l) { $(this).attr('class', '<insertnameofcssclasshere>') })

or

$('li').each(function(l) { $(this).css(<insertcssstuff here>) })

Of course, these would apply it to all li elements on your page. You would want to use more specific or contained selectors if you want to affect just some of them.

于 2011-08-22T17:29:09.263 に答える