jquery – HTML中的数字嵌套排序列表

前端之家收集整理的这篇文章主要介绍了jquery – HTML中的数字嵌套排序列表前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个嵌套的有序列表。
  1. <ol>
  2. <li>first</li>
  3. <li>second
  4. <ol>
  5. <li>second nested first element</li>
  6. <li>second nested secondelement</li>
  7. <li>second nested thirdelement</li>
  8. </ol>
  9. </li>
  10. <li>third</li>
  11. <li>fourth</li>
  12. </ol>

当前,嵌套元素从1开始再次返回,例如。

>第一
>第二

>第二个嵌套的第一个元素
>第二个嵌套的第二个元素
>第二个嵌套的第三个元素

>第三
>第四

我想要的是第二个元素编号如下:

>第一
>第二

2.1。第二嵌套第一元素

2.2。第二嵌套第二元素

2.3。第二嵌套第三元素
>第三
>第四

有没有办法做到这一点?

解决方法

这里有一个在所有浏览器中都有效的示例。纯CSS方法适用于实际的浏览器(即IE6 / 7以外的所有内容), jQuery代码覆盖了不支持的。它在 SSCCE的味道,你可以只是copy’n’paste’nrrun没有改变。
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <title>SO question 2729927</title>
  5. <script src="http://code.jquery.com/jquery-latest.min.js"></script>
  6. <script>
  7. $(document).ready(function() {
  8. if ($('ol:first').css('list-style-type') != 'none') { /* For IE6/7 only. */
  9. $('ol ol').each(function(i,ol) {
  10. ol = $(ol);
  11. var level1 = ol.closest('li').index() + 1;
  12. ol.children('li').each(function(i,li) {
  13. li = $(li);
  14. var level2 = level1 + '.' + (li.index() + 1);
  15. li.prepend('<span>' + level2 + '</span>');
  16. });
  17. });
  18. }
  19. });
  20. </script>
  21. <style>
  22. html>/**/body ol { /* Won't be interpreted by IE6/7. */
  23. list-style-type: none;
  24. counter-reset: level1;
  25. }
  26. ol li:before {
  27. content: counter(level1) ". ";
  28. counter-increment: level1;
  29. }
  30. ol li ol {
  31. list-style-type: none;
  32. counter-reset: level2;
  33. }
  34. ol li ol li:before {
  35. content: counter(level1) "." counter(level2) " ";
  36. counter-increment: level2;
  37. }
  38. ol li span { /* For IE6/7. */
  39. margin: 0 5px 0 -25px;
  40. }
  41. </style>
  42. </head>
  43. <body>
  44. <ol>
  45. <li>first</li>
  46. <li>second
  47. <ol>
  48. <li>second nested first element</li>
  49. <li>second nested second element</li>
  50. <li>second nested third element</li>
  51. </ol>
  52. </li>
  53. <li>third</li>
  54. <li>fourth</li>
  55. </ol>
  56. </body>
  57. </html>

猜你在找的jQuery相关文章