html标题自动生成树形目录,利用原生JS自动生成文章标题树的实例

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

代码如下:

HTML代码:

二级标题

三级标题

hello hello hello hello hello hello hello hello hello hello hello hello

三级标题

三级标题

三级标题

三级标题

hello hello hello hello hello hello hello hello hello hello hello

二级标题

三级标题

三级标题

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

三级标题

三级标题

二级标题

三级标题

三级标题

二级标题

三级标题

三级标题

二级标题

三级标题

三级标题

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 = ("" + hgroup[i].innerHTML +"");

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);

}

完整实例代码

原生JS实现自动生成文章标题树

* {

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%;

}

二级标题

三级标题

hello
hello
hello
hello
hello
hello
hello
hell
o hel
lo hell
o he
llo hello

三级标题

三级标题

三级标题

三级标题

hello hello hello hello hello hello hello hello hello hello hello

二级标题

三级标题

三级标题

world w
orld
world world wo
rld world wo
rld world world wor
ld world world
world
worl
d world
w
orld
world wo
rld wor
ld world wor
ld world worl
d w
or
ld
world
world
world
wo
rld wo
rld w
orld w
orld

三级标题

三级标题

二级标题

三级标题

三级标题

二级标题

三级标题

三级标题

二级标题

三级标题

三级标题

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 = ("" + hgroup[i].innerHTML +"");

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);

}

总结

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值