javascript动态创建对象
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <script type="text/javascript"> function btnClick() { var div = document.getElementById("divMian"); var input = document.createElement("input"); //根据标签名创建DOM input.type = "button"; input.value = "动态创建按钮"; div.appendChild(input); } function btnClick1() { var link = document.getElementById("Link1"); alert(link.innerHTML); alert(link.innerText); } function createTable() { //冒号的前面为Key var data = { "百度": "http://www.baidu.com", "新浪": "http://www.sina.com" }; var div = document.getElementById("table"); for (var Key in data) { var tr = document.createElement("tr"); var value = data[Key]; var td1 = document.createElement("td"); td1.innerText = Key; tr.appendChild(td1); var td2 = document.createElement("td"); td2.innerHTML = "<a href='" + value + "'>" + value + "</a>"; tr.appendChild(td2); div.appendChild(tr); } } function key() { var data = { "百度": "http://www.baidu.com", "新浪": "http://www.sina.com" }; for (var va in data) { alert(va); } } </script> </head> <body> <div id="divMian"> </div> <input type="button" onclick="btnClick()" value="创建按钮" /> <a href="http://baidu.com" id="Link1">传<font color="red">智</font>播客</a> <input type="button" onclick="btnClick1()" value="innerText与innerHTML" /> <br /> <table id="table"> </table> <input type="button" value="创建" onclick="createTable()" /><br /> <input type="button" value="输出key" onclick="key()" /> </body> </html>