为什么要优化

cnblog用了一段时间,发现好多问题

  • 编辑器功能不足,影响编辑效率
  • 美化不足,影响视觉体验

功能优化

自动添加目录

根据h2,h3标签自动生成目录,点击目录的指定条目可以跳转的指定类容,每个内容小标题上都有回到顶部目录按钮。

我只做了两级目录,只有h2存在时才生成目录,且存在h3时,h3是h2的子目录,代码如下:

<script language="javascript" type="text/javascript">
//生成目录索引列表
function GenerateContentList()
{
    var jquery_h23_list = $('#likecs_post_body h2,#likecs_post_body h3');//h2和h3为章节的子母标题
    var go_top = false;
    for(var i =0;i<jquery_h23_list.length;i++)
    {
        if(jquery_h23_list[i].tagName == 'H2')
        {
            go_top = true;
            break;
        }
    }

    if((jquery_h23_list.length>0) && (go_top == true))
    {
        var content = '<a name="_labelTop"></a>';
        content    += '<div >;
        content    += '<p style="font-size:18px"><b>阅读目录</b></p>';
        content    += '<ul>';
        var big_section = '';

        for(var i =0;i<jquery_h23_list.length;i++)
        {
            var go_to_top = '<div style="text-align: right"><a href="#_labelTop">回到顶部</a><a name="_label' + i + '"></a></div>';
            $(jquery_h23_list[i]).before(go_to_top);
            if(jquery_h23_list[i].tagName == 'H2')
            {
                if(big_section != '')
                {
                    big_section += '</ul></li>';
                    content += big_section;
                }
                big_section = '<li><a href="#_label' + i + '"><strong>' + $(jquery_h23_list[i]).text() + '</strong></a><ul>';
            }
            else if(jquery_h23_list[i].tagName == 'H3')
            {
                if(big_section != '')
                {
                    big_section += '<li><a href="#_label' + i + '">' + $(jquery_h23_list[i]).text() + '</a></li>';
                }
            }
        }
        if(big_section != '')
        {
            big_section += '</ul></li>';
            content += big_section;
        }


        content    += '</ul>';
        content    += '</div>';
        if($('#likecs_post_body').length != 0) 
        {
            $($('#likecs_post_body')[0]).prepend(content);
        }
    }    
}
GenerateContentList();
</script>
View Code

相关文章:

  • 2021-08-27
  • 2021-11-22
  • 2021-08-02
  • 2021-09-17
  • 2021-07-05
  • 2022-02-10
  • 2021-10-04
  • 2022-01-11
猜你喜欢
  • 2021-12-31
  • 2021-12-23
  • 2021-06-25
  • 2021-07-29
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案