利用原生JS自动生成文章标题树的实例

 更新时间:2016年08月22日 11:15:58   投稿:daisy  
网上关于生成文章标题树的示例很多,这篇文章介绍的是利用原生JS实现自动生成文章标题树,实现过程很简单,有需要的可以参考借鉴。

实现原理很简单,就是循环文章模块,并抽取其中的h2、h3标签,将其中的内容赋予给新建的title树。

代码如下:

HTML代码:

<div class="contextBox">
 <div id="article">
  <h2>二级标题</h2>
  <h3>三级标题</h3>
  <p>hello hello hello hello hello hello hello hello hello hello hello hello</p>
  <h3>三级标题</h3>
  <h3>三级标题</h3>
  <h3>三级标题</h3>
  <h3>三级标题</h3>
  <p>hello hello hello hello hello hello hello hello hello hello hello</p>
  <h2>二级标题</h2>
  <h3>三级标题</h3>
  <h3>三级标题</h3>
  <p>world world world world world world world world world world world world world world world world world world world world world world world world world world world world world world world </p>
  <h3>三级标题</h3>
  <h3>三级标题</h3>
  <h2>二级标题</h2>
  <h3>三级标题</h3>
  <h3>三级标题</h3>
  <h2>二级标题</h2>
  <h3>三级标题</h3>
  <h3>三级标题</h3>
  <h2>二级标题</h2>
  <h3>三级标题</h3>
  <h3>三级标题</h3>
 </div>
 <div class="articleMenu-box" id="articleMenu_box">
  <span class="articleMenu-open" id="articleMenu_open"></span>
  <ul class="articleMenu hello" id="articleMenu">
   <span class="articleMenu-close" id="articleMenu_close"></span>
  </ul>
 </div>
</div>

CSS代码:

* {
 margin: 0;
 padding: 0;
 border: 0;
}
body {
 font: 16px/1.5;
}
ul li, ol li {
 list-style: none;
}
.contextBox {
 position: relative;
 width: 960px;
 margin: 0 auto;
}
#article {
 margin-left: 200px;
 border: 1px #eee solid;
 padding: 15px;
}
.articleMenu a {
 text-decoration: none;
 color: #333;
}
.articleMenu a:hover {
 color: #f85455;
}
.articleMenu-box {
 width: 170px;
 position: absolute;
 left: 10px;
 top: 10px;
}
.articleMenu {
 padding: 10px;
 border: 1px solid #ccc;
 box-shadow: 2px 2px 2px #eee;
}
.titleH2, .titleH3 {
 line-height: 1.5em;
}
.titleH2 {
 font-weight: bold;
}
.titleH3 {
 margin-left: 20px;
}
.articleMenu .articleMenu-close, .articleMenu-open {
 display: inline-block;
 position: absolute;
 right: 0;
 top: 0;
 height: 44px;
 width: 44px;
 cursor: pointer;
}
.articleMenu-open {
 background: url("http://www.dengzhr.com/wp-content/themes/dengzhr/images/icon_articleMenu_open.png") no-repeat 50% 50%;
 display: none;
}
.articleMenu .articleMenu-close {
 background: url("http://www.dengzhr.com/wp-content/themes/dengzhr/images/icon_articleMenu_close.png") no-repeat 50% 50%;
}

JavaScript代码:

var article = document.getElementById("article");
var articleHgroupMenu = document.getElementById("articleMenu");

// 关闭和展开文档树
var articleMenu_open = document.getElementById("articleMenu_open");
var articleMenu_close = document.getElementById("articleMenu_close");
articleMenu_close.onclick = function() {
 articleHgroupMenu.style.display = "none";
 articleMenu_open.style.display = "block";
};
articleMenu_open.onclick = function() {
 articleHgroupMenu.style.display = "block";
 articleMenu_open.style.display = "none";
};

//
titleHgroup(article, articleHgroupMenu, "titleH2", "titleH3");

// 获得obj下的直接子元素中为标题h2~h3的标题元素
// 参数说明:hgroupParent为包含h2和h3的直接父元素;MenuList为承载新建文章列表的ul元素;
// h2ClassName、h3ClassName分别为新建文章列表中对应h2、h3的li自列表的Class属性;
function titleHgroup(hgroupParent, MenuList, h2ClassName, h3ClassName) {
 var hgroup = hgroupParent.children;

 // 创建文档片段,来包裹自动生成的h2、h3对应生成的li列表
 var fragment = document.createDocumentFragment();
 for(i = 0; i < hgroup.length && hgroup[i].nodeType === 1; i++) {

  // 为对应类型的标题生成li列表
  // 参数说明:hType为标题的类型如h1~h6;className为标题对应的li列表的class属性值;
  function titleToList(hType, className) {
   var li = document.createElement("li");
   li.className = className;

   // 为li标签内部添加a标签,用锚点进行定位;
   hgroup[i].id= hType + i;
   li.innerHTML = ("<a href='#" + hType + i + "'>" + hgroup[i].innerHTML +"</a>");
   fragment.appendChild(li);
  }

  // 当遍历中标题元素为h2时,调用titleToList(hType, className)新增对应的li列表;
  if(hgroup[i].nodeName.toLowerCase() == "h2") {
   titleToList("h2", h2ClassName);
  }

  // 当遍历中标题元素为h3时,调用titleToList(hType, className)新增对应的li列表;
  if(hgroup[i].nodeName.toLowerCase() == "h3") {
   titleToList("h3", h3ClassName);
  }
 }
 // 将承载好对应li元素集合的文档片段fragment添加到DOM(即在DOM中包裹li列表的父元素)中去;
 MenuList.appendChild(fragment);
}

完整实例代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>原生JS实现自动生成文章标题树</title>
<style type="text/css">
* {
 margin: 0;
 padding: 0;
 border: 0;
}
body {
 font: 16px/1.5;
}
ul li, ol li {
 list-style: none;
}
.contextBox {
 position: relative;
 width: 960px;
 margin: 0 auto;
}
#article {
 margin-left: 200px;
 border: 1px #eee solid;
 padding: 15px;
}
.articleMenu a {
 text-decoration: none;
 color: #333;
}
.articleMenu a:hover {
 color: #f85455;
}
.articleMenu-box {
 width: 170px;
 position: absolute;
 left: 10px;
 top: 10px;
}
.articleMenu {
 padding: 10px;
 border: 1px solid #ccc;
 box-shadow: 2px 2px 2px #eee;
}
.titleH2, .titleH3 {
 line-height: 1.5em;
}
.titleH2 {
 font-weight: bold;
}
.titleH3 {
 margin-left: 20px;
}
.articleMenu .articleMenu-close, .articleMenu-open {
 display: inline-block;
 position: absolute;
 right: 0;
 top: 0;
 height: 44px;
 width: 44px;
 cursor: pointer;
}
.articleMenu-open {
 background: url("http://www.dengzhr.com/wp-content/themes/dengzhr/images/icon_articleMenu_open.png") no-repeat 50% 50%;
 display: none;
}
.articleMenu .articleMenu-close {
 background: url("http://www.dengzhr.com/wp-content/themes/dengzhr/images/icon_articleMenu_close.png") no-repeat 50% 50%;
}
</style>

</head>

<body>
<div class="contextBox">
 <div id="article">
  <h2>二级标题</h2>
  <h3>三级标题</h3>
  <p>hello<br /> hello<br /> hello<br /> hello<br /> hello<br /> hello<br /><br /> hello<br /> hell<br />o hel<br />lo hell<br />o he<br />llo hello</p>
  <h3>三级标题</h3>
  <h3>三级标题</h3>
  <h3>三级标题</h3>
  <h3>三级标题</h3>
  <p>hello hello hello hello hello hello hello hello hello hello hello</p>
  <h2>二级标题</h2>
  <h3>三级标题</h3>
  <h3>三级标题</h3>
  <p>world w<br />orld <br />world world wo<br />rld world wo<br />rld world world wor<br />ld world world <br />world <br />worl<br />d world<br /> w<br />orld <br />world wo<br />rld wor<br />ld world wor<br />ld world worl<br />d w<br />or<br />ld<br /> <br />world <br />world <br />world<br /> <br />wo<br />rld wo<br />rld w<br />orld w<br />orld </p>
  <h3>三级标题</h3>
  <h3>三级标题</h3>
  <h2>二级标题</h2>
  <h3>三级标题</h3>
  <h3>三级标题</h3>
  <h2>二级标题</h2>
  <h3>三级标题</h3>
  <h3>三级标题</h3>
  <h2>二级标题</h2>
  <h3>三级标题</h3>
  <h3>三级标题</h3>
 </div>
 <div class="articleMenu-box" id="articleMenu_box">
  <span class="articleMenu-open" id="articleMenu_open"></span>
  <ul class="articleMenu hello" id="articleMenu">
   <span class="articleMenu-close" id="articleMenu_close"></span>
  </ul>
 </div>
</div>
<script type="text/javascript">
var article = document.getElementById("article");
var articleHgroupMenu = document.getElementById("articleMenu");

// 关闭和展开文档树
var articleMenu_open = document.getElementById("articleMenu_open");
var articleMenu_close = document.getElementById("articleMenu_close");
articleMenu_close.onclick = function() {
 articleHgroupMenu.style.display = "none";
 articleMenu_open.style.display = "block";
};
articleMenu_open.onclick = function() {
 articleHgroupMenu.style.display = "block";
 articleMenu_open.style.display = "none";
};

//
titleHgroup(article, articleHgroupMenu, "titleH2", "titleH3");

// 获得obj下的直接子元素中为标题h2~h3的标题元素
// 参数说明:hgroupParent为包含h2和h3的直接父元素;MenuList为承载新建文章列表的ul元素;
// h2ClassName、h3ClassName分别为新建文章列表中对应h2、h3的li自列表的Class属性;
function titleHgroup(hgroupParent, MenuList, h2ClassName, h3ClassName) {
 var hgroup = hgroupParent.children;

 // 创建文档片段,来包裹自动生成的h2、h3对应生成的li列表
 var fragment = document.createDocumentFragment();
 for(i = 0; i < hgroup.length && hgroup[i].nodeType === 1; i++) {

  // 为对应类型的标题生成li列表
  // 参数说明:hType为标题的类型如h1~h6;className为标题对应的li列表的class属性值;
  function titleToList(hType, className) {
   var li = document.createElement("li");
   li.className = className;

   // 为li标签内部添加a标签,用锚点进行定位;
   hgroup[i].id= hType + i;
   li.innerHTML = ("<a href='#" + hType + i + "'>" + hgroup[i].innerHTML +"</a>");
   fragment.appendChild(li);
  }

  // 当遍历中标题元素为h2时,调用titleToList(hType, className)新增对应的li列表;
  if(hgroup[i].nodeName.toLowerCase() == "h2") {
   titleToList("h2", h2ClassName);
  }

  // 当遍历中标题元素为h3时,调用titleToList(hType, className)新增对应的li列表;
  if(hgroup[i].nodeName.toLowerCase() == "h3") {
   titleToList("h3", h3ClassName);
  }
 }
 // 将承载好对应li元素集合的文档片段fragment添加到DOM(即在DOM中包裹li列表的父元素)中去;
 MenuList.appendChild(fragment);
}
</script>
</body>
</html>

总结

以上就是利用原生JS自动生成文章标题树的全部内容,希望本文的内容对大家能有所帮助,如果有疑问可以留言讨论。

相关文章

  • javascript类型系统_正则表达式RegExp类型详解

    javascript类型系统_正则表达式RegExp类型详解

    下面小编就为大家带来一篇javascript类型系统_正则表达式RegExp类型详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-06-06
  • uniapp实现钉钉扫码登录示例代码

    uniapp实现钉钉扫码登录示例代码

    由于uniapp暂无钉钉授权登录所以本文将钉钉扫码登录作为网页嵌入uniapp,最终实现钉钉扫码登录app,本文通过实例代码给大家介绍uniapp钉钉扫码登录功能,感兴趣的朋友一起看看吧
    2021-12-12
  • Ajax实现搜索框提示功能

    Ajax实现搜索框提示功能

    这篇文章介绍了Ajax实现搜索框提示功能的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-04-04
  • ES6数组与对象的解构赋值详解

    ES6数组与对象的解构赋值详解

    这篇文章主要介绍了ES6数组与对象的解构赋值,结合实例形式详细分析了ES6中数组与对象的解构赋值原理、用法及相关操作注意事项,需要的朋友可以参考下
    2019-06-06
  • 微信小程序修改数组长度的问题的解决

    微信小程序修改数组长度的问题的解决

    这篇文章主要介绍了微信小程序修改数组长度的问题的解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-12-12
  • js实现简单div拖拽功能实例

    js实现简单div拖拽功能实例

    这篇文章主要介绍了js实现简单div拖拽功能的方法,实例分析了javascript针对div层拖拽的实现技巧,需要的朋友可以参考下
    2015-05-05
  • javascript表单控件实例讲解

    javascript表单控件实例讲解

    这篇文章主要为大家详细介绍了javascript表单控件实例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-09-09
  • JS使用Expires max-age判断缓存过期的浏览器实例

    JS使用Expires max-age判断缓存过期的浏览器实例

    这篇文章主要为大家介绍了JS使用Expires max-age判断缓存过期的浏览器实例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-11-11
  • 小程序实现多列选择器

    小程序实现多列选择器

    这篇文章主要为大家详细介绍了小程序实现多列选择器的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-02-02
  • js实现的捐赠管理完整实例

    js实现的捐赠管理完整实例

    这篇文章主要介绍了js实现的捐赠管理完整实例,包括了html页面、js脚本及css样式的完整实现代码,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-01-01

最新评论