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


Posted in Javascript onAugust 22, 2016

实现原理很简单,就是循环文章模块,并抽取其中的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 相关文章推荐
Javascript继承(上)——对象构建介绍
Nov 08 Javascript
详解jquery uploadify 上传文件
Nov 09 Javascript
Node.js编码规范
Jul 14 Javascript
JS实现网页标题栏显示当前时间和日期的完整代码
Nov 02 Javascript
快速使用Bootstrap搭建传送带
May 06 Javascript
深入浅析jQuery对象$.html
Aug 22 Javascript
js阻止移动端页面滚动的两种方法
Jan 25 Javascript
jQuery实现可兼容IE6的遮罩功能详解
Sep 19 jQuery
Vue2.0 实现单选互斥的方法
Apr 13 Javascript
Javascript实现秒表倒计时功能
Nov 17 Javascript
vue中实现Monaco Editor自定义提示功能
Jul 05 Javascript
Vue鼠标滚轮滚动切换路由效果的实现方法
Aug 04 Vue.js
jQuery使用deferreds串行多个ajax请求
Aug 22 #Javascript
JavaScript代码里的判断小结
Aug 22 #Javascript
angularjs 源码解析之scope
Aug 22 #Javascript
js表单元素checked、radio被选中的几种方法(详解)
Aug 22 #Javascript
js严格模式总结(分享)
Aug 22 #Javascript
xtemplate node.js 的使用方法实例解析
Aug 22 #Javascript
node.js express安装及示例网站搭建方法(分享)
Aug 22 #Javascript
You might like
PHP XML数据解析代码
2010/05/26 PHP
解决yii2左侧菜单子级无法高亮问题的方法
2016/05/08 PHP
Laravel如何使用Redis共享Session
2018/02/23 PHP
Javascript 获取链接(url)参数的方法
2009/02/15 Javascript
Js表格万条数据瞬间加载实现代码
2014/02/20 Javascript
Jquery之Bind方法参数传递与接收的三种方法
2014/06/24 Javascript
使用jquery.qrcode生成彩色二维码实例
2014/08/08 Javascript
js钢琴按钮波浪式图片排列效果代码分享
2015/08/26 Javascript
详解JavaScript的Date对象(制作简易钟表)
2020/04/07 Javascript
探究JavaScript函数式编程的乐趣
2015/12/14 Javascript
PHP+jquery+ajax实现分页
2016/12/09 Javascript
jQuery Ajax使用FormData上传文件和其他数据后端web.py获取
2017/06/11 jQuery
JS实现弹出下载对话框及常见文件类型的下载
2017/07/13 Javascript
基于JavaScript实现带数据验证和复选框的表单提交
2017/08/23 Javascript
vue之父子组件间通信实例讲解(props、$ref、$emit)
2018/05/22 Javascript
使用iView Upload 组件实现手动上传图片的示例代码
2018/10/01 Javascript
[01:02:09]Liquid vs TNC 2019国际邀请赛淘汰赛 胜者组 BO3 第二场 8.21
2020/07/19 DOTA
Python中的闭包详细介绍和实例
2014/11/21 Python
Python解决鸡兔同笼问题的方法
2014/12/20 Python
Python生成器(Generator)详解
2015/04/13 Python
Python基础教程之浅拷贝和深拷贝实例详解
2017/07/15 Python
利用python将xml文件解析成html文件的实现方法
2017/12/22 Python
简单谈谈Python的pycurl模块
2018/04/07 Python
对python3中pathlib库的Path类的使用详解
2018/10/14 Python
使用Selenium破解新浪微博的四宫格验证码
2018/10/19 Python
实例详解Python模块decimal
2019/06/26 Python
Pandas+Matplotlib 箱式图异常值分析示例
2019/12/09 Python
使用python的pyplot绘制函数实例
2020/02/13 Python
详解Python 函数参数的拆解
2020/09/02 Python
Answear匈牙利:来自全球200多个知名时尚品牌
2017/04/21 全球购物
《藏戏》教学反思
2014/02/11 职场文书
夫妻分居协议书范本
2014/11/28 职场文书
借条格式范本
2015/05/25 职场文书
django上传文件的三种方式
2021/04/29 Python
oracle删除超过N天数据脚本的方法
2022/02/28 Oracle
Python编写车票订购系统 Python实现快递收费系统
2022/08/14 Python