首頁 > web前端 > css教學 > 如何在 HTML 中對嵌套有序列表進行編號?

如何在 HTML 中對嵌套有序列表進行編號?

Susan Sarandon
發布: 2024-11-15 08:36:02
原創
1115 人瀏覽過

How to Number Nested Ordered Lists in HTML?

Numbering Nested Ordered Lists in HTML

To number nested ordered lists in HTML, a combination of CSS and JavaScript can be employed. This approach works consistently in modern browsers, including those that do not support advanced CSS properties.

CSS Approach:

  1. Set the list-style-type of the main ordered list to none using CSS:

1

2

3

ol {

  list-style-type: none;

}

登入後複製
  1. Use the counter-reset and counter-increment CSS properties to generate the numbers. For instance, the first level of nesting should increment a counter named "level1":

1

2

3

4

ol:before {

  content: counter(level1) ". ";

  counter-increment: level1;

}

登入後複製
  1. For nested ordered lists, reset the counter to start a new sequence:

1

2

3

4

ol li ol {

  list-style-type: none;

  counter-reset: level2;

}

登入後複製
  1. Generate the numbers for the nested list using the previously incremented counter:

1

2

3

4

ol li ol li:before {

  content: counter(level1) "." counter(level2) " ";

  counter-increment: level2;

}

登入後複製

jQuery Approach (IE6/7 Support):

  1. Wrap nested list elements with a to apply styling:

1

2

3

4

5

6

7

$('ol ol').each(function(i, ol) {

  ol = $(ol);

  ol.children('li').each(function(i, li) {

    li = $(li);

    li.prepend('<span>' + level2 + '</span>');

  });

});

登入後複製
  1. Style the elements to provide the desired numbering format:

1

2

3

ol li span {

  margin: 0 5px 0 -25px;

}

登入後複製

By combining these CSS and JavaScript techniques, you can effectively number nested ordered lists in HTML, ensuring consistent formatting across major browsers.

以上是如何在 HTML 中對嵌套有序列表進行編號?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板