例 1.6(replaceChildIEFF.html)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
</HEAD>
<BODY>
<div id="contain">
<div id="a">a </div>
<div id="b">b </div>
<div id="c">c </div>
</div>
<script>
var contain = document.getElementById("contain");
var b = document.getElementById("b");
var p = document.createElement("p");/*这句话后创建了一个<p>*/
p.innerHTML="pText";
/*
Node.replaceChild() (Method)
A means of replacing child objects with new nodes and discarding the old ones.
The new node must be created using the createElement method.
Property/method value type: Node object
JavaScript syntax: - myNode.replaceChild(newNode, oldNode)
Argument list: newNode The node to be placed into the hierarchy
oldNode The node to be replaced
*/
contain.replaceChild( p , b );
</script>
</BODY>
</HTML>
更多请见:https://blog.youkuaiyun.com/qq_44594371/article/details/103064096
本文通过一个具体示例展示了如何使用JavaScript的Node.replaceChild()方法来替换HTML文档中的DOM元素。示例中,一个<div>元素被一个新创建的<p>元素所替代,清晰地解释了此方法的使用步骤。
620

被折叠的 条评论
为什么被折叠?



