1<html>
2<head>
3<title>t1</title>
4<script type="text/javascript">
5 function addNode()
6 {var newP = document.createElement("p");
7 var textNode = document.createTextNode(" This is a new text node");
8 newP.appendChild(textNode);
9 document.getElementById("firstP").appendChild(newP); }
10</script> </head>
11<body> <p id="firstP">firstP<p> </body>
12</html>
13
1function create(htmlStr) {
2 var frag = document.createDocumentFragment(),
3 temp = document.createElement('div');
4 temp.innerHTML = htmlStr;
5 while (temp.firstChild) {
6 frag.appendChild(temp.firstChild);
7 }
8 return frag;
9}
10
11var fragment = create('<div>Hello!</div><p>...</p>');
12// You can use native DOM methods to insert the fragment:
13document.body.insertBefore(fragment, document.body.childNodes[0]);
14