版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<meta charset="utf-8" />
</head>
<body>
<button id="btn1">动态创建表格</button>
<script type="text/javascript">
window.onload = function () {
var jsonstr = {'百度':'http://www.baidu.com','饿了么':'http://www.ele.me','谷歌':'http://www.g.cn'}
document.getElementById('btn1').onclick = function () {
var table = document.createElement('table');
table.width = '300px';
table.border = '1px';
for (var key in jsonstr) {//根据数据的属性个数来确定表格的行数
var tr = document.createElement('tr');
var td1 = document.createElement('td');
var td2 = document.createElement('td');
td1.innerHTML = key;//将属性名添加到第一列
td2.innerHTML = '<a href="' + jsonstr[key] + '" target="_blank">' + key + '</a>';//将连接添加到第二列
tr.appendChild(td1);//将第一列添加到第一行
tr.appendChild(td2);//将第二列添加到第一行
table.appendChild(tr);//将每一行添加到表中
}
document.body.appendChild(table);//循环完成后,将表添加到body中
};
};
</script>
</body>
</html>