2017-03-07 13 views
1

私はコメントコメント間を移動HTMLコード(税込。コメント)

<p>Regular text</p> 
<p>Regular text</p> 
<!--Special Content--> 
<p>Special Content 1</p> 
<p>Special Content 2</p> 
<!--/Special Content--> 
<p>Regular text</p> 
<p>Regular text</p> 
<p id="reference">Place special content with comments below this <p> element</p> 

間のHTML特殊なコンテンツの下に、私は<div>にコメント自身を含むコメント<!--Special Content--><!--/Special Content-->間のHTMLコンテンツをラップする必要がありますしているとそれを<p id="reference">要素の下に移動します。

私は実際のHTMLテンプレートにアクセスできないため、JavaScriptまたはJQuery経由で行う必要があります。

これまでのコメントを含めてコンテンツをラップする方法を見つけることができませんでした。

最終的な結果は次のようになります。

<p>Regular text</p> 
<p>Regular text</p> 
<p>Regular text</p> 
<p>Regular text</p> 
<p id="reference">Place special content with comments below this <p> element</p> 
<div id="wrapper"> 
    <!--Special Content--> 
    <p>Special Content 1</p> 
    <p>Special Content 2</p> 
    <!--/Special Content--> 
</div> 

私はあなたのアイデアを感謝しています。

+0

あなたは、これまで何を試してみましたか?私はjQueryがコメントノードを "見つける"ための最良の選択であるかどうかはわかりません - それはバニラJSで簡単かもしれません。 – CBroe

+0

@Rory McCrossan:情報と追跡用です。追加のコンテンツは「特別なコンテンツ」領域に動的に追加され、そのコメントなしで移動すると他のアクション/イベントを参照するために「特別なコンテンツ」を使用する他の人には問題が発生します –

+0

@CBroe:プレーンJSも問題ありません。私はURの方向を感謝します。 –

答えて

2

var dataElem = document.getElementById("data"); 
 

 
var doMove = false; 
 
var toMove = []; 
 

 
for (var i = 0; i < dataElem.childNodes.length; i++) { 
 
    var node = dataElem.childNodes[i]; 
 
    if (node.nodeType === Node.COMMENT_NODE && node.data === "Special Content") { 
 
    doMove = true; 
 
    } 
 
    if (doMove) { 
 
    toMove.push(node); 
 
    } 
 
    if (node.nodeType === Node.COMMENT_NODE && node.data === "/Special Content") { 
 
    doMove = false; 
 
    } 
 
} 
 

 
var targetElem = document.createElement("DIV"); 
 
targetElem.setAttribute("id", "wrapper"); 
 
dataElem.insertBefore(targetElem, document.getElementById("reference").nextSibling); 
 
for (var i = 0; i < toMove.length; i++) { 
 
    targetElem.appendChild(toMove[i]); 
 
}
<div id="data"> 
 
    <p>Regular text</p> 
 
    <p>Regular text</p> 
 
    <!--Special Content--> 
 
    <p>Special Content 1</p> 
 
    <p>Special Content 2</p> 
 
    <!--/Special Content--> 
 
    <p>Regular text</p> 
 
    <p>Regular text</p> 
 
    <p id="reference">Place special content with comments below this <p> element</p> 
 
</div>

+0

ありがとう!これは完全に機能します。私は本当にあなたの助けとオリエンテーションに感謝します。 –

0

これは参照IDタグの後にあなたの特別なコメントタグの間のすべてのコンテンツを移動します。

let move = false; 
 
let parse = $('body').contents().each(function(k,v){ 
 

 
\t if(v.nodeType === 8){ 
 
\t \t if(v.nodeValue === "Special Content") move = true; 
 
\t \t else if(v.nodeValue === "/Special Content") move = false; 
 
\t } 
 
\t else if(move && v.nodeType === 1) 
 
\t \t $(v).insertAfter('#reference'); 
 
\t 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<p>Regular text</p> 
 
<p>Regular text</p> 
 
<!--Special Content--> 
 
<p>Special Content 1</p> 
 
<p>Special Content 2</p> 
 
<!--/Special Content--> 
 
<p>Regular text</p> 
 
<p>Regular text</p> 
 
<p id="reference">Place special content with comments below thiselement</p>

+1

移動したノードの順序が間違っています。コメントノードは明示的に要求されたとおりに移動せず、ラッパーdiv ...;には入れません。 – Lucero

+0

ああ、私の味は嫌です。あなたがそれを処理したように見えます。私はあなたの答えをupvoted :-) –

関連する問題