jquery - 如何使用 css 对自定义目录进行编号?

标签 jquery html css

我目前正在使用 Javascript 基于我的 HTML 动态创建目录。

我想为用户提供的选项之一是,当他们创建目录时,他们是否要使用在我的 JSCSS 编号中创建的编号.

我的 JS 编号工作正常,但我的 CSS 编号。 因为我没有使用 ol li 创建我的列表,所以很难用 CSS 选择和编号我的表格。

我在开发控制台中包含了一张我的 table 的图片。如您所见,每个元素都有一个 toc-level 类,然后是一个 number

"main-headings" 的级别为 1,"sub-headings" 的级别为 2,等等。我尝试使用 CSS 计数器来执行此任务,但是我不确定如何正确实现它。我希望 11.11.21.2.1 等编号>“主标题”“副标题”

有没有人有任何想法如何做到这一点?

My Code from the Dev Console

非常感谢。

更新:

我发现这个 CSS 有帮助,但它无法像我的 3 级标题那样对子标题进行编号:

.show-css-numbering {
        counter-reset: heading;
    }

    .toc-level-1:before {
        content: counter(heading)") ";
        counter-increment: heading;
    }

    .toc-level-1 {
        counter-reset: subheading;
    }

    .toc-level-2:before {
        content: counter(heading)"." counter(subheading)") ";
        counter-increment: subheading;
    }

最佳答案

我使用以下格式的 css 来回答我的问题:

   .show-css-numbering {
            counter-reset: heading;
        }

        .toc-level-1:before {
            content: counter(heading)") ";
            counter-increment: heading;
        }

        .toc-level-1 {
            counter-reset: subheadingLVL2;
        }

        .toc-level-2:before {
            content: counter(heading)"." counter(subheadingLVL2)") ";
            counter-increment: subheadingLVL2;
        }

        .toc-level-2 {
            counter-reset: subheadingLVL3;
        }

        .toc-level-3:before {
            content: counter(heading)"." counter(subheadingLVL2)"." counter(subheadingLVL3)") ";
            counter-increment: subheadingLVL3;
        }

        .toc-level-3 {
            counter-reset: subheadingLVL4;
        }

        .toc-level-4:before {
            content: counter(heading)"." counter(subheadingLVL2)"." counter(subheadingLVL3)"." counter(subheadingLVL4)") ";
            counter-increment: subheadingLVL4;
        }

        .toc-level-4 {
            counter-reset: subheadingLVL5;
        }

        .toc-level-5 {
            content: counter(heading)"." counter(subheadingLVL2)"." counter(subheadingLVL3)"." counter(subheadingLVL4)"." counter(subheadingLVL5)") ";
            counter-increment: subheadingLVL5;
        }

        .toc-level-5 {
            counter-reset: subheadingLVL6;
        }

        .toc-level-6 {
            content: counter(heading)"." counter(subheadingLVL2)"." counter(subheadingLVL3)"." counter(subheadingLVL4)"." counter(subheadingLVL5)"." counter(subheadingLVL6)") ";
            counter-increment: subheadingLVL6;
        }

这会生成一个包含正确编号的子标题和标题的列表。一直到第 6 级子标题。之后,您还可以在所有选择器前面包含 .show-css-numbering,以便可以根据需要使用 Jquery 打开和关闭编号.

关于jquery - 如何使用 css 对自定义目录进行编号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51719425/

相关文章:

javascript - 在 Javascript 中切换开/关按钮

javascript - 仅显示段落的前两行

溢出隐藏的 Css 工具提示?

css - 1200 宽及以上的 Twitter Bootstrap 元素溢出页面边缘

javascript - 如何在使用 javascript 创建的不同图像中使用不同的 css 样式?

javascript - JQuery .on ("keydown") 附加到页面时不起作用

jquery - 使用 jquery 选中/取消选中复选框?

jquery - 有没有办法只在页面加载后加载内联 css 背景图像?

html - 如何使此代码响应

html - 作为 Sharepoint 页面的静态 HTML 页面