for an element in DOM,such as an "A" element,how to replace it with a "span" instead?
+6
A:
by using replaceChild():
<html>
<head>
</head>
<body>
<div>
<a id="myAnchor" href="http://www.stackoverflow">StackOveflow</a>
</div>
<script type="text/JavaScript">
var myAnchor = document.getElementById("myAnchor");
var mySpan = document.createElement("span");
mySpan.innerHTML = "replaced anchor!";
myAnchor.parentNode.replaceChild(mySpan, myAnchor);
</script>
</body>
</html>
apphacker
2009-05-09 17:15:41
this example wouldn't work. You should put the script block at the end of the body to make it work. Furthermore, just for fun: try adding the line [alert(myAnchor.innerHTML)] after the operation.
KooiInc
2009-05-10 08:27:05
Ah yeah, good point.
apphacker
2009-05-10 08:43:27
Hahahahahaha. I just did the alert. You sir, are awesome.
apphacker
2009-05-10 08:45:09
Theres a spelling mistake with StackOverflow in the anchor innerText
rahul
2009-05-11 11:50:06
+1
A:
var a = A.parentNode.replaceChild(document.createElement("span"), A);
a is the replaced A element.
Kamarey
2009-05-09 17:20:27