这里替换的是 b 标签 为什么使用的是oldnode.parentNode.replaceChild(newnode,oldnode)而不是oldnode.replaceChild(newnode,oldnode)????
<!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);
document.write(oldnode.parentNode.nodeName);
}
</script>
</body>
</html>Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
replaceChild()函数实现的是子节点的替换,不是自身的替换,要替换oldnode,当然得用oldnode.parentNode结点来调用这个函数