- 没有什么直接的、简单的方法来改变一个元素的类型。
- 改变元素类型的主要手段——克隆
例:
<h1>Changing the type of an element</h1>
<p id="starLinks">
<a href="test1.htm">Sirius</a>
<a href="test2.htm">Achanar</a>
</p>
var div=document.createElement("div");
var p=document.getElementById("starLinks");
for(var i=0;i<p.childNodes.length;i++)
{
var clone=p.childNodes[i].cloneNode(true);//true表示克隆子节点本身
或者div.appendChild(p.firstChild);
div.appendChild(clone);
}
div.id=p.getAttribute("id");
div.className="starLink";
p.parentNode.replaceChild(div,p);
//查看源文件如下:
//<H1>Changing the type of an element</H1>
//<DIV id=starLinks class=starLink>
<A href="test1.htm">Sirius</A>
<A href="test2.htm">Achanar</A>
</DIV>