Javascript 是否可以将唯一的CSS类附加到多个<;李>;元素,还是使用Jquery对其重新排序?

Javascript 是否可以将唯一的CSS类附加到多个<;李>;元素,还是使用Jquery对其重新排序?,javascript,jquery,css,squarespace,Javascript,Jquery,Css,Squarespace,目前正在Squarespace.com平台上工作,我想重新排序一个动态博客类别索引,默认为字母排序。这是Squarespace生成代码的方式: <div id="moduleContentWrapper11663355" class="widget-wrapper widget-type-journalarchive"> <div id="moduleContent11663355"> <ul class="archive-item-list

目前正在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>

  • (1)
  • (1)
  • (1)
是否可以使用Jquery对条目进行重新排序,或者更糟糕的是,我是否可以向每个
  • 附加一个唯一的CSS类,以便使用CSS对每个条目进行操作


    谢谢

    使用jQuery对条目进行排序可以采用两种不同的方式,但是如果您只想将CSS附加到每个条目上,可以采用两种不同的方式

    var i = 1;
    $('.archive-item-list-pt li').each(function(item) {
       $(item).addClass('myClass'+i);
    });
    
    $('li').each(function(l) { $(this).attr('class', '<insertnameofcssclasshere>') })
    
    $('li').each(函数(l){$(this.attr('class',''))
    

    $('li').each(函数(l){$(this.css()})
    

    当然,这些将应用于页面上的所有li元素。如果只想影响其中一些选择器,您可能需要使用更具体或包含的选择器。

    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); });
    
    var mylist=$('moduleContent11663355>ul');
    var listitems=mylist.children('li').get();
    排序(函数(a,b){
    var compA=$(a.text().toUpperCase();
    var compB=$(b.text().toUpperCase();
    返回(compAcompB)?1:0;
    })
    $.each(列表项,函数(idx,itm){mylist.append(itm);});
    
    多亏了

    这是小提琴

    更新

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

    一个具有jquery链接热度的

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

    根据任意键功能就地排序项目


    仅供参考-您的标记无效。确认!谢谢,我手工做了
  • 部分,对不起!固定的对不起,我没有提到每个类必须是不同的(匹配博客类别)。我尝试了addClass,它非常适合在所有
  • 中添加一个类,但我需要为每个类添加一个不同的类,这样我就可以使用CSS有选择地隐藏它们。这非常有效,但现在我真的会很痛苦。。。是否可以根据类别名称附加唯一标识符?例如。。。班级=第一类?我之所以需要这样做,是因为如果我在每个类中添加一个数字,然后通过CSS进行样式设置,然后我们添加一个新的类别,数字将发生变化,并且我以前所做的任何样式都将不匹配。如果我根据每个类别名称使用一个唯一的类,CSS的样式将永远不会改变。再次感谢你的帮助。这也很有效。问题是,如果页面上的类别数发生变化,则类将发生变化。这将消除我所做的任何样式。是否可以根据类别名称附加唯一标识符?例如。。。class=类别1(因为这是类别名称和URL)?谢谢你的帮助。
    $('.archive-item-list-pt li').each(function(i,j) {
       var cls=$(this).children("a").text().toLowerCase();
        var idx = cls.replace(' ','-');
    
        $(this).addClass(idx);
    });
    
    $('.archive-item-list-pt li').each(function(i,j) {
       var cls=$(this).children("a").text().toLowerCase().replace(' ','-');  
       $(this).addClass(cls);
    });