如何删除节点
1<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2<html xmlns="http://www.w3.org/1999/xhtml" >
3<head>
4 <title>无标题页</title>
5
6 <script type="text/javascript" language="javascript">
7
8 window.onload=initAll;
9
10 function initAll()
11 {
12 document.getElementsByTagName("form")[0].onsubmit=addNode;
13 document.getElementById("delNode").onclick=delNode;
14 }
15
16 //addNode()函数
17 function addNode()
18 {
19 var inText=document.getElementById("TextArea1").value;
20 var newText=document.createTextNode(inText);
21 var newGraf=document.createElement("p");
22 newGraf.appendChild(newText);
23 var docBody=document.getElementsByTagName("body")[0];
24 docBody.appendChild(newGraf);
25 return false;
26 }
27
28 //delNode()函数
29 function delNode()
30 {
31 var allParagraphs=document.getElementsByTagName("p");
32 var pLen=allParagraphs.length;
33 //alert(pLen);
34 if(pLen>1)
35 {
36 var lastP=allParagraphs.item(pLen-1);
37 var docBody=document.getElementsByTagName("body")[0];
38 docBody.removeChild(lastP);
39 }
40 return false;
41 }
42
43 </script>
44
45</head>
46<body>
47
48<form id="form1" method="post" action="#">
49
50 <textarea id="TextArea1" cols="20" rows="2"></textarea>
51 <input id="Submit1" type="submit" value="submit" />
52
53</form>
54<p><a id="delNode" href="#">Delete the last paragraph !</a></p>
55
56
57</body>
58</html>
59
2<html xmlns="http://www.w3.org/1999/xhtml" >
3<head>
4 <title>无标题页</title>
5
6 <script type="text/javascript" language="javascript">
7
8 window.onload=initAll;
9
10 function initAll()
11 {
12 document.getElementsByTagName("form")[0].onsubmit=addNode;
13 document.getElementById("delNode").onclick=delNode;
14 }
15
16 //addNode()函数
17 function addNode()
18 {
19 var inText=document.getElementById("TextArea1").value;
20 var newText=document.createTextNode(inText);
21 var newGraf=document.createElement("p");
22 newGraf.appendChild(newText);
23 var docBody=document.getElementsByTagName("body")[0];
24 docBody.appendChild(newGraf);
25 return false;
26 }
27
28 //delNode()函数
29 function delNode()
30 {
31 var allParagraphs=document.getElementsByTagName("p");
32 var pLen=allParagraphs.length;
33 //alert(pLen);
34 if(pLen>1)
35 {
36 var lastP=allParagraphs.item(pLen-1);
37 var docBody=document.getElementsByTagName("body")[0];
38 docBody.removeChild(lastP);
39 }
40 return false;
41 }
42
43 </script>
44
45</head>
46<body>
47
48<form id="form1" method="post" action="#">
49
50 <textarea id="TextArea1" cols="20" rows="2"></textarea>
51 <input id="Submit1" type="submit" value="submit" />
52
53</form>
54<p><a id="delNode" href="#">Delete the last paragraph !</a></p>
55
56
57</body>
58</html>
59