zoukankan      html  css  js  c++  java
  • 为博客园添加目录的方法总结

    注: 本文来源 《 为博客园添加目录的方法总结


    参考链接:
    http://www.cnblogs.com/xdp-gacl/p/3718879.html#2937655
    http://www.cnblogs.com/zzqcn/p/4657124.html
    http://www.cnblogs.com/jiangz/p/3734968.html

    第一种:在正文上方直接添加目录

    1. 申请开通js权限

    默认是不支持,需要打开博客园后台,进入“设置”标签页,点击“申请开通js权限”,并注明用途
    Paste_Image.png
    如果想加快申请速度,也可以再向官方发个邮件(邮箱是contact@cnblogs.com),邮件也需要注明用途,快的话1小时就能搞定了

    2. 添加js脚本到“页脚Html代码”

    打开博客园后台,进入“设置”标签页,在最下面的“页脚Html代码”对应的编辑框粘贴下面的js代码,然后点“保存”按钮保存。
    (如果没有开通js权限,此处无法添加)
    Paste_Image.png

    Paste_Image.png

      1 <script language="javascript" type="text/javascript">
      2 // 生成目录索引列表
      3 // ref: http://www.cnblogs.com/wangqiguo/p/4355032.html
      4 // modified by: zzq
      5 function GenerateContentList()
      6 {
      7     var mainContent = $('#cnblogs_post_body');
      8     var h2_list = $('#cnblogs_post_body h2');//如果你的章节标题不是h2,只需要将这里的h2换掉即可
      9 
     10     if(mainContent.length < 1)
     11         return;
     12 
     13     if(h2_list.length>0)
     14     {
     15         var content = '<a name="_labelTop"></a>';
     16         content += '<div id="navCategory" style="color:#152e97;">';
     17         content += '<p style="font-size:18px;"><b>目录</b></p>';
     18         content += '<ul>';
     19         for(var i=0; i<h2_list.length; i++)
     20         {
     21             var go_to_top = '<div style="text-align: right;"><a href="#_labelTop" style="color:#f68a33">回到顶部</a><a name="_label' + i + '"></a></div>';
     22             $(h2_list[i]).before(go_to_top);
     23 
     24             var h3_list = $(h2_list[i]).nextAll("h3");
     25             var li3_content = '';
     26             for(var j=0; j<h3_list.length; j++)
     27             {
     28                 var tmp = $(h3_list[j]).prevAll('h2').first();
     29                 if(!tmp.is(h2_list[i]))
     30                     break;
     31                 var li3_anchor = '<a name="_label' + i + '_' + j + '"></a>';
     32                 $(h3_list[j]).before(li3_anchor);
     33                 li3_content += '<li><a href="#_label' + i + '_' + j + '">' + $(h3_list[j]).text() + '</a></li>';
     34             }
     35 
     36             var li2_content = '';
     37             if(li3_content.length > 0)
     38                 li2_content = '<li><a href="#_label' + i + '">' + $(h2_list[i]).text() + '</a><ul>' + li3_content + '</ul></li>';
     39             else
     40                 li2_content = '<li><a href="#_label' + i + '">' + $(h2_list[i]).text() + '</a></li>';
     41             content += li2_content;
     42         }
     43         content += '</ul>';
     44         content += '</div><p>&nbsp;</p>';
     45         content += '<hr style="height:1px;border:none;border-top:1px dashed #0066CC;"/>';
     46         if($('#cnblogs_post_body').length != 0 )
     47         {
     48             $($('#cnblogs_post_body')[0]).prepend(content);
     49         }
     50     }
     51 }
     52 
     53 GenerateContentList();
     54 </script>

    3. 按格式写文章

    在写新博文的时候,注意按照你js脚本里设定的格式来划分章节(这里只设定了h2,h3,对应普通编辑器中的二级标题和三级标题,对应markdown中的##和###)。当然,以前发布的文章如果有h2,h3,也会自动生成目录索引。
    效果如下:
    Paste_Image.png

    第二种:在文章右上角添加目录导航

    1. 申请开通js权限

    同上

    2. 添加css代码到“页面定制CSS代码”

    Paste_Image.png

      1 /*生成博客目录的CSS*/
      2 #uprightsideBar{
      3     font-size:12px;
      4     font-family:Arial, Helvetica, sans-serif;
      5     text-align:left;
      6     position:fixed;/*将div的位置固定到距离top:50px,right:0px的位置,这样div就会处在最右边的位置,距离顶部50px*/
      7     top:50px;
      8     right:0px;
      9      auto;
     10     height: auto;
     11 }
     12 #sideBarTab{
     13     float:left;
     14     30px;
     15     border:1px solid #e5e5e5;
     16     border-right:none;
     17     text-align:center;
     18     background:#ffffff;
     19 }
     20 
     21 #sideBarContents{
     22     float:left;
     23     overflow:auto;
     24     overflow-x:hidden;!important;
     25     200px;
     26     min-height:108px;
     27     max-height:460px;
     28     border:1px solid #e5e5e5;
     29     border-right:none;
     30     background:#ffffff;
     31 }
     32 #sideBarContents dl{
     33     margin:0;
     34     padding:0;
     35 }
     36 
     37 #sideBarContents dt{
     38     margin-top:5px;
     39     margin-left:5px;
     40 }
     41 
     42 #sideBarContents dd, dt {
     43     cursor: pointer;
     44 }
     45 
     46 #sideBarContents dd:hover, dt:hover {
     47     color:#A7995A;
     48 }
     49 #sideBarContents dd{
     50     margin-left:20px;
     51 }

    3. 添加js脚本到“页首Html代码”

    Paste_Image.png

      1 <script type="text/javascript">
      2 /*
      3     功能:生成博客目录的JS工具
      4     测试:IE8,火狐,google测试通过
      5     孤傲苍狼
      6     2014-5-11
      7 */
      8 var BlogDirectory = {
      9     /*
     10         获取元素位置,距浏览器左边界的距离(left)和距浏览器上边界的距离(top)
     11     */
     12     getElementPosition:function (ele) {
     13         var topPosition = 0;
     14         var leftPosition = 0;
     15         while (ele){
     16             topPosition += ele.offsetTop;
     17             leftPosition += ele.offsetLeft;
     18             ele = ele.offsetParent;
     19         }
     20         return {top:topPosition, left:leftPosition};
     21     },
     22 
     23     /*
     24     获取滚动条当前位置
     25     */
     26     getScrollBarPosition:function () {
     27         var scrollBarPosition = document.body.scrollTop || document.documentElement.scrollTop;
     28         return  scrollBarPosition;
     29     },
     30 
     31     /*
     32     移动滚动条,finalPos 为目的位置,internal 为移动速度
     33     */
     34     moveScrollBar:function(finalpos, interval) {
     35 
     36         //若不支持此方法,则退出
     37         if(!window.scrollTo) {
     38             return false;
     39         }
     40 
     41         //窗体滚动时,禁用鼠标滚轮
     42         window.onmousewheel = function(){
     43             return false;
     44         };
     45 
     46         //清除计时
     47         if (document.body.movement) {
     48             clearTimeout(document.body.movement);
     49         }
     50 
     51         var currentpos =BlogDirectory.getScrollBarPosition();//获取滚动条当前位置
     52 
     53         var dist = 0;
     54         if (currentpos == finalpos) {//到达预定位置,则解禁鼠标滚轮,并退出
     55             window.onmousewheel = function(){
     56                 return true;
     57             }
     58             return true;
     59         }
     60         if (currentpos < finalpos) {//未到达,则计算下一步所要移动的距离
     61             dist = Math.ceil((finalpos - currentpos)/10);
     62             currentpos += dist;
     63         }
     64         if (currentpos > finalpos) {
     65             dist = Math.ceil((currentpos - finalpos)/10);
     66             currentpos -= dist;
     67         }
     68 
     69         var scrTop = BlogDirectory.getScrollBarPosition();//获取滚动条当前位置
     70         window.scrollTo(0, currentpos);//移动窗口
     71         if(BlogDirectory.getScrollBarPosition() == scrTop)//若已到底部,则解禁鼠标滚轮,并退出
     72         {
     73             window.onmousewheel = function(){
     74                 return true;
     75             }
     76             return true;
     77         }
     78 
     79         //进行下一步移动
     80         var repeat = "BlogDirectory.moveScrollBar(" + finalpos + "," + interval + ")";
     81         document.body.movement = setTimeout(repeat, interval);
     82     },
     83 
     84     htmlDecode:function (text){
     85         var temp = document.createElement("div");
     86         temp.innerHTML = text;
     87         var output = temp.innerText || temp.textContent;
     88         temp = null;
     89         return output;
     90     },
     91 
     92     /*
     93     创建博客目录,
     94     id表示包含博文正文的 div 容器的 id,
     95     mt 和 st 分别表示主标题和次级标题的标签名称(如 H2、H3,大写或小写都可以!),
     96     interval 表示移动的速度
     97     */
     98     createBlogDirectory:function (id, mt, st, interval){
     99          //获取博文正文div容器
    100         var elem = document.getElementById(id);
    101         if(!elem) return false;
    102         //获取div中所有元素结点
    103         var nodes = elem.getElementsByTagName("*");
    104         //创建博客目录的div容器
    105         var divSideBar = document.createElement('DIV');
    106         divSideBar.className = 'uprightsideBar';
    107         divSideBar.setAttribute('id', 'uprightsideBar');
    108         var divSideBarTab = document.createElement('DIV');
    109         divSideBarTab.setAttribute('id', 'sideBarTab');
    110         divSideBar.appendChild(divSideBarTab);
    111         var h2 = document.createElement('H2');
    112         divSideBarTab.appendChild(h2);
    113         var txt = document.createTextNode('目录导航');
    114         h2.appendChild(txt);
    115         var divSideBarContents = document.createElement('DIV');
    116         divSideBarContents.style.display = 'none';
    117         divSideBarContents.setAttribute('id', 'sideBarContents');
    118         divSideBar.appendChild(divSideBarContents);
    119         //创建自定义列表
    120         var dlist = document.createElement("dl");
    121         divSideBarContents.appendChild(dlist);
    122         var num = 0;//统计找到的mt和st
    123         mt = mt.toUpperCase();//转化成大写
    124         st = st.toUpperCase();//转化成大写
    125         //遍历所有元素结点
    126         for(var i=0; i<nodes.length; i++)
    127         {
    128             if(nodes[i].nodeName == mt|| nodes[i].nodeName == st)
    129             {
    130                 //获取标题文本
    131                 var nodetext = nodes[i].innerHTML.replace(/</?[^>]+>/g,"");//innerHTML里面的内容可能有HTML标签,所以用正则表达式去除HTML的标签
    132                 nodetext = nodetext.replace(/ /ig, "");//替换掉所有的 
    133                 nodetext = BlogDirectory.htmlDecode(nodetext);
    134                 //插入锚        
    135                 nodes[i].setAttribute("id", "blogTitle" + num);
    136                 var item;
    137                 switch(nodes[i].nodeName)
    138                 {
    139                     case mt:    //若为主标题 
    140                         item = document.createElement("dt");
    141                         break;
    142                     case st:    //若为子标题
    143                         item = document.createElement("dd");
    144                         break;
    145                 }
    146 
    147                 //创建锚链接
    148                 var itemtext = document.createTextNode(nodetext);
    149                 item.appendChild(itemtext);
    150                 item.setAttribute("name", num);
    151                 item.onclick = function(){        //添加鼠标点击触发函数
    152                     var pos = BlogDirectory.getElementPosition(document.getElementById("blogTitle" + this.getAttribute("name")));
    153                     if(!BlogDirectory.moveScrollBar(pos.top, interval)) return false;
    154                 };
    155 
    156                 //将自定义表项加入自定义列表中
    157                 dlist.appendChild(item);
    158                 num++;
    159             }
    160         }
    161 
    162         if(num == 0) return false;
    163         /*鼠标进入时的事件处理*/
    164         divSideBarTab.onmouseenter = function(){
    165             divSideBarContents.style.display = 'block';
    166         }
    167         /*鼠标离开时的事件处理*/
    168         divSideBar.onmouseleave = function() {
    169             divSideBarContents.style.display = 'none';
    170         }
    171 
    172         document.body.appendChild(divSideBar);
    173     }
    174 
    175 };
    176 
    177 window.onload=function(){
    178     /*页面加载完成之后生成博客目录*/
    179     BlogDirectory.createBlogDirectory("cnblogs_post_body","h2","h3",20);
    180 }
    181 </script>

    4. 按格式写文章

    同上
    效果如下:
    Paste_Image.png

  • 相关阅读:
    web----WSGI
    ovs 实现vlan隔离(一)
    ovs流表机制(四)用vxlan实现不同网段通信
    ovs流表机制(四)用vxlan实现同网段通信
    ovs 流表机制(三)--group表
    ovs 流表机制(二)-OVS流表table之间的跳转
    ovs 流表机制(一)
    euler ironic镜像驱动问题(一)镜像启动失败报dracut initqueue timeout
    ovs流表
    arm64 uefi启动
  • 原文地址:https://www.cnblogs.com/ios9/p/8629278.html
Copyright © 2011-2022 走看看