码迷,mamicode.com
首页 > 编程语言 > 详细

javascript动态创建对象

时间:2014-05-22 16:24:20      阅读:267      评论:0      收藏:0      [点我收藏+]

标签:c   java   ext   http   color   a   

<!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>

javascript动态创建对象,布布扣,bubuko.com

javascript动态创建对象

标签:c   java   ext   http   color   a   

原文地址:http://www.cnblogs.com/sumg/p/3742763.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!