代码
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
4 <head>
5 <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
6 <title>DOM添加元素,使用节点属性</title>
7 <script language="javascript" type="text/javascript">
8 //涂聚文 geovindu@163.com
9 var nr = 1;
10 function addItem() {
11 var list = document.getElementById("list");
12 var newNode = document.createElement("li");
13 var newLink = document.createElement("a");
14 newLink.setAttribute("href", "http://www.dusystem.com/");
15 nr++;
16 var newTextNode = document.createTextNode("Item " + nr);
17 newLink.appendChild(newTextNode);
18 newNode.appendChild(newLink);
19 list.appendChild(newNode);
20 }
21 </script></head>
22
23 <body>
24
25 <ul id="list"><li>Item 1</li></ul>
26 <input type="button" onclick="addItem();" value="Add item" />
27 </body>
28
29 </html>
30
2 <html xmlns="http://www.w3.org/1999/xhtml">
3
4 <head>
5 <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
6 <title>DOM添加元素,使用节点属性</title>
7 <script language="javascript" type="text/javascript">
8 //涂聚文 geovindu@163.com
9 var nr = 1;
10 function addItem() {
11 var list = document.getElementById("list");
12 var newNode = document.createElement("li");
13 var newLink = document.createElement("a");
14 newLink.setAttribute("href", "http://www.dusystem.com/");
15 nr++;
16 var newTextNode = document.createTextNode("Item " + nr);
17 newLink.appendChild(newTextNode);
18 newNode.appendChild(newLink);
19 list.appendChild(newNode);
20 }
21 </script></head>
22
23 <body>
24
25 <ul id="list"><li>Item 1</li></ul>
26 <input type="button" onclick="addItem();" value="Add item" />
27 </body>
28
29 </html>
30