2017-03-15 3 views
0

これは私が処理しようとしている段落です。私はここではJavaScriptinsertBefore()の親として使用するもの

<p id="mySpecialParagraph">Well hello there, user</p> 

を使用して、それが最後の行にpId.parentNode.insertBeforeのために代わりに使用すべきか、関連するjavascriptのである前に、新しい段落を挿入しようとするのですか?

var pId = document.getElementById("mySpecialParagraph").nodeValue; 

     var pNew = document.createElement("P"); 
     var text = "This should apperar before the other paragraph"; 
     var t = document.createTextNode(text); 

     pNew.appendChild(t); 

     pId.parentNode.insertBefore(pNew, pId); 

答えて

1

それはそのままです。 getElementById()の後にnodeValueを取り除くだけで済みます。

var pId = document.getElementById("mySpecialParagraph"); 
 

 
var pNew = document.createElement("P"); 
 
var text = "This should apperar before the other paragraph"; 
 
var t = document.createTextNode(text); 
 

 
pNew.appendChild(t); 
 

 
pId.parentNode.insertBefore(pNew, pId);
<p id="mySpecialParagraph">Well hello there, user</p>

関連する問題