使用JS,文章内容页根据H标签自动生成目录

迅睿框架 更新 2026-05-27 14:27:05 又菜又爱玩

获取文章所有的h标签,生成目录,增加跳转js

点击 当前页内跳转 

自动识别当前位置 进行赋值active


个人比较菜,扣了半天才搞出来,拿去参考,有不足之处大家帮忙指正


右侧目录代码 主要是id

<div class="col-lg-3 col-md-12 col-12">
      <div class="header">
  <h6 class="title title-1">目录</h6>
   </div>
    <div id="groupfile">
                               
    </div>
</div>

左侧文章代码

 <div class="entry-wrapper">
         <div class="entry-content u-text-format u-clearfix" id="content">
              {$content}    
     </div>    
     </div>

js代码 

// 首先根据id获取id下所有H标签
var content=document.getElementById("content"); 
var headList = [...content.querySelectorAll('h1,h2,h3,h4,h5,h6')];
  // 将获取的H标签构造成树
  var root = {
      children: []
  };
  var h = {
      node: headList[0],
      children: [],
      parent: root
  };
  root.children.push(h);
  headList.reduce(function (pre, cur) {
      var n = {
          node: cur,
          children: []
      }
      while (h.node.localName[1] - n.node.localName[1] !== -1) {
          h = h.parent;
          if (h === root) {
              break;
          }
      }
      n.parent = h;
      h.children.push(n);
      h = n;
      return h;
  });
  // 给H标签加id
  var index = 0;
  function createList(list) {
      var text = list.reduce(function (pre, cur) {
           cur.node.id = 'header' + index++;
          var childText // 判断该H标签有没有子层H标签
          if (cur.children.length) {
              childText = createList(cur.children)
          } else {
              childText = ''
          }
         
          pre += ` 
                  <li class="groupfile-list">
                 <a class="catalog-tag" href="#${cur.node.id}">
                   ${cur.node[xss_clean]}
                   </a>
                 ${childText}
                </li>
               
                `
                //目录的列表
          return pre;
      }, '');
      var text = ` <ul class=""> ${text}  </ul>`
      return text;
  }
//循环出目录
  var content = createList(root.children);
  document.getElementById('groupfile')[xss_clean] = content;
  
 var left_div_top = 100; //左侧边栏距离顶部高度
    var right_nav_top = 150;     //右侧标题距离浏览器窗口顶部距离(距离顶部多少就切换左侧标题高亮)
    var groupfile_list = $("#groupfile .groupfile-list>a"); // 获取左侧标题列表dome
    var groupfile_list1 = $("#groupfile .groupfile-list"); // 获取左侧标题列表dome,用来给li加active
        //滚动条事件
        $(window).scroll(function () {
            var windouWidth = $(this).width();//获取浏览器自身宽度
            var windouHeight = $(this).height();//获取浏览器自身高度
            //获取滚动条的滑动距离
            var scroH = $(this).scrollTop();
            // 滚动js
            if ($(".main").offset()) {
                // js判断标题高亮
                $(headList).each(function (index, element) {
                    if ($("#header" + (index)).offset().top + windouHeight - right_nav_top < scroH + windouHeight) {
                        $(groupfile_list1[index-1]).removeClass("active"),
                        $(groupfile_list1[index]).addClass("active")
                    } else {
                        $(groupfile_list1[index]).removeClass("active")
                    }
                });
                
             
            }
        });
        // 目录跳转js
        groupfile_list.click(function (e) {
            e.preventDefault();
            var annname = $(this).attr("href");
            $('html, body').animate({ scrollTop: $(annname).offset().top - left_div_top }, 100)
        });  

image

方案

  • 阿陆来 #1 · 2023-02-22 09:57:43
    感谢你的技术分享!!!很不错!
  • 又菜又爱玩 #2 · 2023-02-22 10:20:48
    补充:左侧粘性自己搞,我是用的是开源jq插件 GitHub - garand/sticky: jQuery Plugin for Sticky Objects
  • 善建者 #3 · 2023-02-23 22:59:42
    我也遇到了这个问题,感谢分享!
  • yudeleishu #4 · 2023-02-24 10:07:37
    感谢你的技术分享!
  • 麦没有兜 #5 · 2023-06-29 15:53:21
    感谢你的技术分享!
  • Hi #6 · 2023-07-04 14:09:15
    感谢你的技术分享!
  • demo1452 #7 · 2023-07-06 21:50:29
    看看如何使用
  • 张先生 #8 · 2023-07-20 09:01:09
    大佬 厉害了。
  • 天客隆模板网 #9 · 2023-07-26 20:17:51
    看看如何使用
  • 屺无心 #10 · 2023-08-24 10:29:51
    感谢你的技术分享,学习学习!!!
  • 嘟嘟小将-爆仓专业户 #11 · 2023-08-25 20:35:14
    感谢你的技术分享!
  • chaoyong #12 · 2023-11-30 13:13:50
    感谢大佬分享!
  • heimeng #13 · 2023-12-11 21:20:57
    看看咋写的
  • 白嫖怪 #14 · 2023-12-19 15:44:23
    让我康康怎么个事儿
  • 洛沙 #15 · 2024-01-04 22:05:25
    😀 😎 看看,学习下
  • cuinew520 #16 · 2024-03-05 23:41:42
    感谢您的无私奉献!
  • cuinew520 #17 · 2024-03-05 23:42:42
    又菜又爱玩 感谢您的无私奉献
  • 学无止境 #18 · 2024-03-16 10:33:45
    看看怎么个思路,谢谢。
  • 小豪子 #19 · 2024-10-09 13:35:46
    感谢您的无私奉献!感谢您的无私奉献!
  • 小豪子 #20 · 2024-10-09 13:49:52

    image
    用你的例子出来这个
  • 我爱迅睿CMS #21 · 2024-10-09 14:52:27
    • 感谢你的技术分享!!!很不错!
  • nemoccc #22 · 2024-10-29 16:41:10
    感谢您的无私奉献!
  • 学习狂人 #23 · 2024-10-30 11:10:42
    学习中,谢谢分享
  • 棒棒糖 #24 · 2024-11-01 17:03:38
    这个好东西,要学习
  • 求知若渴虚心若愚 #25 · 2024-11-28 08:28:58
    感谢你的技术分享,学习学习!!!
  • jnsirman #26 · 2024-12-02 14:42:40
    找了半天终于找到了,感谢
  • 凌云大大 #27 · 2024-12-04 08:18:27
    找了半天终于找到了,感谢
  • 莫愁 #28 · 2025-02-06 22:47:19
    感谢你的技术分享!
  • Alian #29 · 2025-02-18 17:57:51
    感谢分享 正好需要
  • 风凌君 #30 · 2025-02-19 16:17:57
    看看能不能用上,参考参考,谢谢分享
  • 远山红叶 #31 · 2025-03-18 23:22:01
    看看能不能用上,参考参考,感谢大神
  • yunduoba #32 · 2025-11-02 16:15:48
    感谢你的技术分享,学习学习!!!
  • 明明 #33 · 2026-05-27 14:27:05
    感谢你的技术分享,学习学习!!!

迅睿系统漏洞

GeoSeo收录宝
GeoSeo收录宝(2026-09-01)
升级 ai智能发布功能 升级 支持8国语言后端设置和前端输出功能 ...
Word发布批量上传
Word发布批量上传(2026-08-31)
版本覆盖问题重发
多语言翻译Seolang
多语言翻译Seolang(2026-08-31)
新增 LLM 大模型对话翻译模块 配置层强类型化重构 URL 模...
自定义存储
自定义存储(2026-08-30)
同步服务器端通用版实现,统一管理端、CMS 端和浏览器直传的提示与错...
GeoSeo工具箱
GeoSeo工具箱(2026-08-29)
新增 bing推送 新增 IndexNow推送 新增 baidu...
外贸多语言建站
外贸多语言建站(2026-08-27)
新增阿里云专业版入口 新增阿里云自动识别字符格式
Querylist渐进式采集
Querylist渐进式采集(2026-08-26)
增加范围截取规则
实时任务
实时任务(2026-08-26)
补充并整理完整使用说明、历史版本更新日志,统一帮助页样式。