javascript 动态创建表格 js foreach

使用insertRow和insertCell方法
<html>
<head> <title>Test javascript method </title>

<body>
<script language="javascript">
 function LoadData() {
  var data = {"腾讯":"http://www.qq.com","百度":"http://www.baidu.com","新浪":"http://www.sina.com"};
  var tableLinks = document.getElementById("tableLinks");
for(var key in data){
  var value = data[key];
   
  var tr = tableLinks.insertRow(-1);
  var td1 = tr.insertCell(-1);
  td1.innerText =key;
   
  var td2 = tr.insertCell(-1);
  td2.innerHTML = "<a href='"+value+"'>" +value+ "</a>";
  }
}
</script>
<table id="tableLinks"></table>
<input type="button" value="创建表格" onclick="LoadData()" /></body>

你可能感兴趣的:(JavaScript)