jQuery   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了jquery – HTML中的数字嵌套排序列表大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个嵌套的有序列表。
<ol>
  <li>first</li>
  <li>second
  <ol>
    <li>second nested first element</li>
    <li>second nested secondelement</li>
    <li>second nested thirdelement</li>
  </ol>
  </li>
  <li>third</li>
  <li>fourth</li>
</ol>

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

>第一
>第二

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

>第三
>第四

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

>第一
>第二

2.1。第二嵌套第一元素

2.2。第二嵌套第二元素

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

有没有办法做到这一点?

解决方法

这里有一个在所有浏览器中都有效的示例。纯CSS方法适用于实际的浏览器(即IE6 / 7以外的所有内容), jQuery代码覆盖了不支持的。它在 SSCCE的味道,你可以只是copy’n’paste’nrrun没有改变。
<!doctype html>
<html lang="en">
    <head>
        <title>SO question 2729927</title>
        <script src="http://code.jquery.com/jquery-latest.min.js"></script>
        <script>
            $(document).ready(function() {
                if ($('ol:first').css('list-style-type') != 'none') { /* For IE6/7 only. */
                    $('ol ol').each(function(i,ol) {
                        ol = $(ol);
                        var level1 = ol.closest('li').index() + 1;
                        ol.children('li').each(function(i,li) {
                            li = $(li);
                            var level2 = level1 + '.' + (li.index() + 1);
                            li.prepend('<span>' + level2 + '</span>');
                        });
                    });
                }
            });
        </script>
        <style>
            html>/**/body ol { /* Won't bE interpreted by IE6/7. */
                list-style-type: none;
                counter-reset: level1;
            }
            ol li:before {
                content: counter(level1) ". ";
                counter-increment: level1;
            }
            ol li ol {
                list-style-type: none;
                counter-reset: level2;
            }
            ol li ol li:before {
                content: counter(level1) "." counter(level2) " ";
                counter-increment: level2;
            }
            ol li span { /* For IE6/7. */
                margin: 0 5px 0 -25px;
            }
        </style>
    </head>
    <body>
        <ol>
            <li>first</li>
            <li>second
                <ol>
                    <li>second nested first element</li>
                    <li>second nested second element</li>
                    <li>second nested third element</li>
                </ol>
            </li>
            <li>third</li>
            <li>fourth</li>
        </ol>
    </body>
</html>

大佬总结

以上是大佬教程为你收集整理的jquery – HTML中的数字嵌套排序列表全部内容,希望文章能够帮你解决jquery – HTML中的数字嵌套排序列表所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。