替换元素节点replaceChild()
replaceChild 实现子节点(对象)的替换。返回被替换对象的引用。
语法:
node.replaceChild (newnode,oldnode )
参数:
newnode : 必需,用于替换 oldnode 的对象。
oldnode : 必需,被 newnode 替换的对象。
demo:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
</head>
<body>
<div><b id="oldnode">JavaScript</b>是一个很常用的技术,为网页添加动态效果。</div>
<a href="javascript:replaceMessage()"> 将加粗改为斜体</a>
<script type="text/javascript">
function replaceMessage(){
var newnode = document.createElement("i");
var oldnode = document.getElementById("oldnode");
newnode.innerHTML = oldnode.innerHTML;
oldnode.parentNode.replaceChild(newnode,oldnode);
}
</script>
</body>
</html>