幽灵资源网 Design By www.bzswh.com
实现原理很简单,就是循环文章模块,并抽取其中的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自动生成文章标题树的全部内容,希望本文的内容对大家能有所帮助,如果有疑问可以留言讨论。
幽灵资源网 Design By www.bzswh.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
幽灵资源网 Design By www.bzswh.com
暂无评论...
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。