Nested ordered lists

前端 未结 3 1909
青春惊慌失措
青春惊慌失措 2020-12-06 14:36

I need a nested list with subitem numbering, like this:

1. Item 1
  1.1 - Subitem 1
  1.2 - Subitem 2
  1.3 - Subitem 3
  1.4 - Subitem 4
  1.5 - Subitem 5
2         


        
3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-06 14:50

    You can use CSS to do so:

    OL { counter-reset: item }
    LI { display: block }
    LI:before { content: counter(item) ". - "; counter-increment: item }
    LI LI:before { content: counters(item, ".") " - "; counter-increment: item }
    

    But it requires support for counter and counters.


    Edit    Here’s a jQuery approach similar to dcneiner’s but with no limitation to depth:

    function foo($ol, counters) {
        counters = counters || [];
        $ol.each(function(i) {
            var $this = $(this);
            $this.children("li").each(function(i) {
                var $this = $(this);
                $this.prepend(counters.concat([i+1]).join(".") + " ");
                $this.children("ol").each(function(j) {
                    foo($(this), counters.concat([i+1]));
                });
            });
        });
    }
    foo($("ol:not(li > ol)"));
    

提交回复
热议问题