create element1:
var anchorText=document.createTextNode("monoceros");
var newAnchor = document.createElement("a");
newAnchor.href="http://www.baidu.com";//给新建的超链接节点添加链接属性
newAnchor.appendChild(anchorText);
var parent=document.getElementById("starLinks");
var newChild=parent.appendChild(newAnchor);
<h1>Creating elements and test nodes</h1>
<p id="starLinks">
<a href="test1.htm">Sirius</a>
</p>
create element2:
var anchorText=document.createTextNode("monoceros");
var newAnchor = document.createElement("a");
newAnchor.href = "http://www.baidu.com";
newAnchor.appendChild(anchorText);
var existingAnchor=document.getElementById("sirius");
var parent=existingAnchor.parentNode;
var newChild=parent.insertBefore(newAnchor,existingAnchor);
<h1>Creating elements and text nodes</h1>
<p id="starLinks">
<a id="sirius" href="test1.htm">Sirius</a>
</p>
create element3:
var anchorText=document.createTextNode("monoceros");
var newAnchor = document.createElement("a");
newAnchor.href = "http://www.baidu.com";
newAnchor.appendChild(anchorText);
var existingAnchor=document.getElementById("sirius");
var parent=existingAnchor.parentNode;
var newChild=parent.replaceChild(newAnchor,existingAnchor);
<h1>Creating elements and text nodes</h1>
<p id="starLinks">
<a id="sirius" href="test1.htm">Sirius</a>
</p>