[英]Populate HTML Table using Javascript
I want to populate a predefined html table using JavaScript: 我想使用JavaScript填充预定义的html表:
<table>
<tr>
<th scope="col">ID</th>
<th scope="col">Name</th>
</tr>
<div id='data'/>
</table>
using 运用
document.getElementById('data').innerHTML = ....
But since <div>
is not allowed inside of <table>
above code doesn't work. 但由于<div>
不允许在<table>
上面代码不起作用。 What is the correct way to achieve this? 实现这一目标的正确方法是什么?
Instead of Div you can use tr and td. 而不是Div你可以使用tr和td。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html>
<head>
<script type="text/javascript">
function addRow(content,morecontent)
{
if (!document.getElementsByTagName) return;
tabBody=document.getElementsByTagName("tbody").item(0);
row=document.createElement("tr");
cell1 = document.createElement("td");
cell2 = document.createElement("td");
textnode1=document.createTextNode(content);
textnode2=document.createTextNode(morecontent);
cell1.appendChild(textnode1);
cell2.appendChild(textnode2);
row.appendChild(cell1);
row.appendChild(cell2);
tabBody.appendChild(row);
}
</script>
</head>
<body>
<table border='1' id='mytable'>
<tbody>
<tr><td>22</td><td>333</td></tr>
<tr><td>22</td><td>333</td></tr>
</tbody>
</table>
<button onClick='addRow("123","456");return false;'>
Add Row</button>
</body>
</html>
In the most basic sense: 在最基本的意义上:
<table>
<tr>
<th>ID</th>
<th>Name</th>
</tr>
<tr>
<td colspan="2">
<div> LOTS O' CONTENT </div>
</td>
</tr>
</table>
You've answered yourself - put the div in the right location - either inside the table or outside. 你已经回答了自己 - 把div放在正确的位置 - 无论是在桌子内还是在外面。 The javascript will work, whether the div will be displayed where you'd like it to, is a different question :) javascript会起作用,div是否会显示在你想要的地方,是一个不同的问题:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.