• 博客园生成3级目录索引js代码


    博客园生成三级目录索引的js代码:

    <!-- 目录索引列表生成 -->
    <script language="javascript" type="text/javascript">
    
    function GenerateContentList()
    {
        if ($('#cnblogs_post_body').length == 0) { return; }
        var jquery_h1_list = $('#cnblogs_post_body h1');
        var jquery_h2_list = $('#cnblogs_post_body h2');
        var jquery_h3_list = $('#cnblogs_post_body h3');
    
        if (jquery_h1_list.length != 0)
        {
            var content = '<a name="_labelTop"></a>';
            content    += '<div id="navCategory">';
            content    += '<p style="font-size:18px"><b>目录</b></p>';
            content += '<ul class="first_class_ul">';
    
            for (var i = 0; i < jquery_h1_list.length; i++)
            {
                var go_to_top = '<div style="text-align: right"><a href="#_labelTop">返回目录</a><a name="_label' + i + '"></a></div>';
                $(jquery_h1_list[i]).before(go_to_top);
                var li_content = '<li><a href="#_label' + i + '">' + $(jquery_h1_list[i]).text() + '</a></li>';
                var nextH1Index = i + 1;
                if (nextH1Index == jquery_h1_list.length) { nextH1Index = 0; }
                var jquery_h2_list = $(jquery_h1_list[i]).nextUntil(jquery_h1_list[nextH1Index], "h2");
                if (jquery_h2_list.length > 0)
                {
                    li_content += '<ul class="second_class_ul">';
                }
                for (var j = 0; j < jquery_h2_list.length; j++)
                {
                    var go_to_top2 = '<div style="text-align: right"><a href="#_labelTop">返回目录</a><a name="_lab2_'+ i + '_' + j + '"></a></div>';
                    $(jquery_h2_list[j]).before(go_to_top2);
                    li_content +='<li><a href="#_lab2_'+ i +'_' + j + '">' + $(jquery_h2_list[j]).text() + '</a></li>';
                    var nextH2Index = j + 1;
                    var next;
                    if (nextH2Index == jquery_h2_list.length) 
                    {
                        if (i + 1 == jquery_h1_list.length)
                        {
                            next = jquery_h1_list[0];
                        }
                        else
                        {
                            next = jquery_h1_list[i + 1];
                        }
                    }
                    else
                    {
                        next = jquery_h2_list[nextH2Index];
                    }
                    var jquery_h3_list = $(jquery_h2_list[j]).nextUntil(next, "h3");
                    if (jquery_h3_list.length > 0)
                    {
                        li_content += '<ul class="third_class_ul">';
                    }
                    for (var k = 0; k < jquery_h3_list.length; k++)
                    {
                        var go_to_third_Content = '<div style="text-align: right"><a href="#_labelTop">返回目录</a><a name="_label3_' + i + '_' + j + '_' + k + '"></a></div>';
                        $(jquery_h3_list[k]).before(go_to_third_Content);
                        li_content += '<li><a href="#_label3_' + i + '_' + j + '_' + k + '">' + $(jquery_h3_list[k]).text() + '</a></li>';
                    }               
                    if (jquery_h3_list.length > 0)
                    {
                        li_content += '</ul>';
                    }
                    li_content += '</li>';
                }
                if (jquery_h2_list.length > 0)
                {
                    li_content +='</ul>';
                }
                li_content +='</li>';
                content += li_content;
            }
            content    += '</ul>';
            content    += '</div>';
        }
        else if (jquery_h2_list.length != 0)
        {
            var content = '<a name="_labelTop"></a>';
            content    += '<div id="navCategory">';
            content    += '<p style="font-size:18px"><b>目录</b></p>';
            content    += '<ul class="second_class_ul">';
            for(var i =0; i < jquery_h2_list.length; i++)
            {
                var go_to_top2 = '<div style="text-align: right"><a href="#_labelTop">返回目录</a><a name="_lab2_'+ i + '_' + j + '"></a></div>';
                $(jquery_h2_list[j]).before(go_to_top2);
                var li_content = '<li><a href="#_lab' + i + '">' + $(jquery_h2_list[i]).text() + '</a></li>';
                var nextH1Index = i + 1;
                if (nextH1Index == jquery_h2_list.length) { nextH1Index = 0; }
                var jquery_h3_list = $(jquery_h2_list[i]).nextUntil(jquery_h2_list[i+1], "h3");
                if(jquery_h3_list.length>0)
                {
                    li_content +='<ul class="third_class_ul">';
                }
                for(var j = 0;j < jquery_h3_list.length;j++)
                {                  
                    var go_to_third_Content = '<div style="text-align: right"><a href="#_labelTop">返回目录</a><a name="_label3_' + i + '_' + j + '_' + k + '"></a></div>';
                    $(jquery_h3_list[k]).before(go_to_third_Content);
                    li_content +='<li><a href="#_lab2'+ i +'_' + j + '">' + $(jquery_h3_list[j]).text() + '</a></li>';
                }
                if(jquery_h3_list.length>0)
                {
                    li_content +='</ul>';
                }
                li_content +='</li>';
                content += li_content;
            }
            content    += '</ul>';
            content    += '</div>'; 
        }
        else if (jquery_h3_list.length != 0) 
        {
            var content = '<a name="_labelTop"></a>';
            content    += '<div id="navCategory">';
            content    += '<p style="font-size:18px"><b>目录</b></p>';
            content    += '<ul>';
            for(var i = 0; i < jquery_h3_list.length; i++)
            {
                var go_to_third_Content = '<div style="text-align: right"><a href="#_labelTop">返回目录</a><a name="_label3_' + i + '_' + j + '_' + k + '"></a></div>';
                $(jquery_h3_list[k]).before(go_to_third_Content);
                var li_content = '<li><a href="#_label' + i + '">' + $(jquery_h3_list[i]).text() + '</a></li>';
                content += li_content;
            }
            content    += '</ul>';
            content    += '</div>';
        }
        else
        {
            return;
        }
    $($('#cnblogs_post_body')[0]).prepend(content);
    }
    GenerateContentList();
    </script>
    

    参考链接:https://www.cnblogs.com/qiuhlee/p/9301120.html

  • 相关阅读:
    mac 10.15.7 修改PATH
    oc 属性类型一般用法
    ubuntu解压zip文件名乱码
    telnet 退出
    docker 根据容器创建镜像
    mac android adb device 没有显示设备
    Yii2 查看所有的别名 alias
    Yii2 App Advanced 添加 .gitignore
    ubuntu 18.04 搜狗突然就提示乱码
    An error occured while deploying the file. This probably means that the app contains ARM native code and your Genymotion device cannot run ARM instructions. You should either build your native code to
  • 原文地址:https://www.cnblogs.com/forever521Lee/p/10718779.html
Copyright © 2020-2023  润新知