This article describes the method of appendChild by javascript. Share it for your reference. The specific analysis is as follows:
The example code for the addition of DOM tree nodes is as follows:
Copy the code as follows: <html>
<head>
<script type="text/javascript">
function t(){
var nodep = document.createElement('p');//Create p node
var art = document.createTextNode('Hello, world');//Create text node
var cont = document.getElementById('container');//Get node
cont.appendChild(nodep);//Add node
nodep.appendChild(art);//Add text nodes
}
</script>
<style type="text/css">
p{width:100px;height:100px;background:green;}
#container p{width:100px;height:100px;background:blue;}
</style>
</head>
<body>
<div id="container"><p>hello world</p>
</div>
<p>Say a few words</p>
<hr />
<button onclick="t()" value="">Add node</button>
</body>
</html>
I hope this article will be helpful to everyone's JavaScript programming.