実装の原則は非常に簡単です。これは、記事モジュールをループし、H2およびH3タグを抽出し、新しく作成されたタイトルツリーにコンテンツを割り当てることです。
コードは次のとおりです。
HTMLコード:
<div> <div id = "article"> <h2> secondaryタイトル</h2> <h3>セカンダリータイトル</hhellohellohellohellohellohellohellohellohellohellohellohellohellohelloもっとhellohellohelloいくつかこんにちはhelloもっとhellohelloいくつかこんにちはいくつかこんにちはhellohelloいくつかこんにちはいくつかこんにちはhellohellohellohellohellohelloいくつかあるいくつかハローハローハローハローハローハローハローハローハローハローハローハローハローハローハローハローハローハローハローハローハローハローハローハローハローハローハローハローハローハローハローこんにちはこんにちはこんにちはこんにちはこんにちはこんにちはこんにちはこんにちはこんにちはこんにちはこんにちはこんにちはこんにちはこんにちはこんにちはこんにちはこんにちはこんにちはこんにちはこんにちはこんにちはこんにちはこんにちはこんにちはこんにちはこんにちはこんにちはこんにちはこんにちはこんにちはこんにちはこんにちはこんにちはこんにちはこんにちはハローハローハローハローハローハローハローハローハローハローハローハローハローハローハローハローハローワールド」ハローワールドワールドワールドワールドワールドワールドワールドワールドワールドワールドワールドワールドワールドワールドワールドワールドワールドワールドワールドワールドワールドワールドワールドワールドワールドワールドワールドワールドワールドワールドワールドワールドワールドワールドワールドワールドワールドワールドワールドワールドワールドワールドワールドワールドワールドワールドワールドワールドワールドワールドワールドワールドワールドワールドワールドワールドワールドワールドワールドワールドワールドワールドワールドワールドワールドワールドワールドワールドワールドワールドワールドワールドワールド世界世界 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 world id = "articlemenu_box"> <span id = "articlemenu_open"> </span> <ul id = "articlemenu"> <span id = "articlemenu_close"> </span> </ul> </div> </div>
CSSコード:
* {マージン:0;パディング:0;境界:0;} body {font:16px/1.5;} ul li、ol li {list-style:none;}。幅:960px;マージン:0 auto;}#article {margin-left:200px;国境:1px #eee solid;パディング:15px;}。articlemenu a {Text-Decoration:none;色:#333;}。articlemenu a:hover {color:#f85455;}。articlemenu-box {width:170px;位置:絶対;左:10px;上:10px;}。articlemenu {padding:10px;ボーダー:1pxソリッド#ccc; Box-shadow:2px 2px 2px #eee;}。titleh2、.titleh3 {line-height:1.5em;}。titleh2{font-weight:bold;}。titleh3 {margin-left:20px;}。位置:絶対;右:0;上:0;高さ:44px;幅:44px;カーソル:pointer;}。articlemenu-open {background:url( "http://www.dengzhr.com/wp-content/themes/dengzhr/images/icon_articlemenu_open.png")無償50%;ディスプレイ:なし;}。articlemenu .articlemenu-close {background:url( "http://www.dengzhr.com/wp-content/themes/dengzhr/images/icon_articlemenu_close.png")no-repeat 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"、 "tittleh3"); // obj //パラメーター説明:hgrouparentはh2およびh3とh3とh3の直接的な要素であるダイレクト要素でタイトルh2〜h3のタイトル要素を取得します。 Menulistは、新しく作成された記事リストを運ぶUL要素です。 // H2ClassNameとH3ClassNameは、新しく作成された記事リストのH2とH3に対応するLIリストのクラス属性です。 function titlehgroup(hgrouparent、menulist、h2classname、h3classname){var hgroup = hgrouparent.children; // h2およびh3 var fragment = document.createdocumentfragment()に対応する自動生成されたLi-listをラップするドキュメントフラグメントを作成します。 for(i = 0; i <hgroup.length && hgroup [i] .nodetype === 1; i ++){//対応するタイプのタイトルのLi-listを生成します//パラメーター説明:HTypeはH1〜H6などのタイトルのタイプです。クラス名は、タイトルに対応するLIリストのクラス属性値です。 function titletolist(htype、classname){var li = document.createelment( "li"); li.classname = className; // LIタグ内にタグを追加し、アンカーで見つけます。 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要素コレクションをDOM(つまり、DOMのLIリストを包む親要素)に担当するドキュメントフラグメントフラグメントを追加します。 menulist.appendchild(fragment);}完全な例コード
< xmlns = "http://www.w3.org/1999/xhtml"> <head> <meta http-equiv = "content-type" content = "text/html; charset = gb2312"/> <title> native js実装記事を生成します。パディング:0;境界:0;} body {font:16px/1.5;} ul li、ol li {list-style:none;}。幅:960px;マージン:0 auto;}#article {margin-left:200px;国境:1px #eee solid;パディング:15px;}。articlemenu a {Text-Decoration:none;色:#333;}。articlemenu a:hover {color:#f85455;}。articlemenu-box {width:170px;位置:絶対;左:10px;上:10px;}。articlemenu {padding:10px;ボーダー:1pxソリッド#ccc; Box-shadow:2px 2px 2px #eee;}。titleh2、.titleh3 {line-height:1.5em;}。titleh2{font-weight:bold;}。titleh3 {margin-left:20px;}。位置:絶対;右:0;高さ:44px;幅:44px;カーソル:pointer;}。articlemenu-open {background:url( "http://www.dengzhr.com/wp-content/themes/dengzhr/images/icon_articlemenu_open.png")無償50%;表示:なし;}。articlemenu .articlemenu-close {background:url( "http://www.dengzhr.com/wp-content/themes/dengzhr/images/icon_articlemenu_close.png")no-repeat 50% "> <H2>セカンダリータイトル< /h2> <h3>セカンダリータイトル< /h3> <p> hello <br /> hello <br /> hello <br /> hello <br /> hello <br /> hello <br /> hello <br /> hello <br /> hello <br /> hello /> hello <br /> hello <br /> hello <br /> hello <br /> /> hello <br /> hello <br /> hello <br /> hello <br /> hello <br /> hello <br /> hello <br /> hello <br /> hello <br /> hello <br /> hello <br /> hello <br /> hello <br /> hello <br /> hello <br /> hello <hellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohelloいくつかこんにちはいくつかhellohellこんにちはこんにちはこんにちはこんにちはこんにちはこんにちはこんにちはこんにちはこんにちはこんにちはこんにちはこんにちはこんにちはこんにちはこんにちはこんにちはこんにちはこんにちはこんにちはこんにちはこんにちはこんにちはこんにちはこんにちはこんにちはこんにちはこんにちはこんにちはこんにちはこんにちはこんにちはこんにちはこんにちは」 world <br /> world <br /> world <br /> w <br /> orld world <br /> w <br /> orld world <br /> w <br /> w <br /> w <br /> w <br /> w <br /> w <br />> w <br />> w <br /> w <br /> w <br /> w <<br /> w <br /> w <br /> w <br /> /> W <BR /> W <BR /> W <BR /> W <BR /> W <BR /> W <BR /> W <BR /> W <BR /> W <BR /> W <BR /> W <BR /> W <BR /> W <BR /> W <BR /> W <BR /> W <BR /> W <BR /> W <BR /> W <BR /> W <BR /> W <BR /> W <BR /> W <BR /> W <BR /> W <BR /> /> W <BR /> W <BR /> W <BR /> W <BR /> W <BR /> W <BR /> W <BR /> W <BR /> W <BR /> W <BR /> W <BR /> W <BR /> W <BR /> W <BR /> W <BR /> W <BR /> W <BR /> W <BR /> W <BR /> W <BR /> W <BR /> W <BR /> W <BR /> W <BR /> W <BR /> /> W <BR /> W <BR /> W <BR /> W <BR /> W <BR /> W <BR /> W <BR /> W <BR /> W <BR /> W <BR /> W <BR /> W <BR /> W <BR /> W <BR /> W <BR /> W <BR /> W <BR /> W <BR /> W <BR /> W <BR /> W <BR /> W <BR /> W <BR /> W <BR /> W <BR /> /> w <br /> w <br /> w <br /> w <br /> w <br /> w <br /> w <br /> w <br /> w <br /> w <br /> w <br /> w <br /> w <br />> w <br /> w <> rld wo <br /> rld w <br />>> w <br /> />>>>>>>> w <br />>>> </p> <h3>レベル3タイトル</h3> <h3>レベル3タイトル</h3> <h2>レベル3タイトル</h2> <h3>レベル3タイトル</h3> <h3>レベル3タイトル</h3> <h3>タイトル</h3> <h3>レベル3レベル3タイトル</h3> </h3> </h3> </h3>タイトル</h3> <h3>レベル3タイトル</h3> <h3>レベル3タイトル</h3> </div> <div id = "articlemenu_open"> <"articlemenu_open"> </span> <ul id = "articlemenu"> <span id = "article_close"> </</</</> 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"、 "tittleh3"); // obj //パラメーターの説明:hgrouparentはh2とh3とh3とhgrupparerentの直接育児要素でタイトルh2〜h3のタイトル要素を取得します。 Menulistは、新しく作成された記事リストをホストするUL要素です。 // H2ClassNameおよびH3ClassNameは、新しく作成された記事リストのH2とH3に対応するLIリストのクラス属性です。 function titlehgroup(hgrouparent、menulist、h2classname、h3classname){var hgroup = hgrouparent.children; // h2およびh3 var fragment = document.createdocumentfragment()に対応する自動生成されたLi-listをラップするドキュメントフラグメントを作成します。 for(i = 0; i <hgroup.length && hgroup [i] .nodetype === 1; i ++){//対応するタイプ//パラメーターのタイトルのLIリストを生成する説明:HTYPEはH1〜H6などのタイトルのタイプです。クラス名は、タイトルに対応するLIリストのクラス属性値です。 function titletolist(htype、classname){var li = document.createelment( "li"); li.classname = className; // LIタグ内にタグを追加し、アンカーで配置します。 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要素コレクションをDOMにホストするドキュメントフラグメントを追加します(つまり、DOMにLIリストを包む親要素)。 Menulist.AppendChild(fragment);} </script> </body> </html>要約します
上記は、ネイティブJSを使用して記事タイトルツリーを自動的に生成するすべてのコンテンツです。この記事の内容がすべての人に役立つことを願っています。質問がある場合は、メッセージを残して議論することができます。