1.首先在html中为表格的添加位置设置id
即是在html的body标签内部写一个div标签表明表格要添加到此div的内部。如下
<div id="tdl"><div>
2.在javascript中写添加表格的语句
若在当前html文件中,则写在<script>标签内部,如
复制代码 代码如下:
<script type="text/javascript" >
document.getElementById("tbl").innerHTML="<table><tr><td></td></tr></table>" //此处添加的表格可根据自己需要创建
</script>
若是通过引入js文件,则在js文件(假设是test.js)中直接写如下语句
复制代码 代码如下:
document.getElementById("tbl").innerHTML="<table><tr><td></td></tr></table>"
然后再引入自己的html文件
复制代码 代码如下:
<script type="text/javascript" src="test.js"></script>
二、 动态添加表格行
1.首先在html中为表格行的添加位置设置id,此位置必须是<tbody>内部(不是特别准确,但根据我的测试就得到此结论,有其他的方法请留言,谢谢),如下
复制代码 代码如下:
<table>
<thead></thead>
<tfoot><tfoot>//tfoot与thead是与tbody配套使用,但我在写的时候,没用也可以。
<tbody id="rows"></tbody>
</table>
[\s\S ]*\n
2.在javascript内容中,要先创建行和单元格,再在<.tbody>中添加行,如下
[code]
row=document.createElement("tr")//创建行
td1=document.createElement("tr")//创建单元格
td1.appendChild(document.createTextNode("content"))//为单元格添加内容
row.appendChild(td1)//将单元格添加到行内
document.getElementById("rows").append(row)//将行添加到<tbody>中
<!DOCTYPE HTML><html>
<head>
<meta charset=UTF-8>
<title>table</title>
<style>
table {
margin:auto
width: 60%
border: 1px solid black
border-collapse: collapse
}
table caption {
color: blue
font: 34px consolas bold
}
table th, table td {
border: 1px solid black
text-align:center
}
table th {
font: 20px consolas bold
background-color: gray
}
table tr:nth-child(n+2){
background-color: cyan
}
table tr:nth-child(2n+2) {
background-color: red
}
</style>
<script>
var data = [
{id: "001", fullname: "张三", sex: "男", score: [98,33,48]},
{id: "002", fullname: "李四", sex: "w", score: [11,22,33]},
{id: "003", fullname: "kyo", sex: "m", score: [22,33,55]},
{id: "004", fullname: "yamazaki", sex: "w", score: [99, 100, 80]}
]
var Student = function (id, fullname, sex, score)
{
this.id = id
this.fullname = fullname
this.sex = sex
this.score = score
}
var Score = function (chinese, math, english)
{
this.chinese = chinese
this.math = math
this.english = english
}
var students = []
for (var i = 0 i < data.length i++)
{
var d = data[i]
var s = d["score"]
students.push(new Student (d["id"], d["fullname"], d["sex"], new Score(s[0], s[1], s[2])))
}
onload = function ()
{
var table = document.createElement("table")
var tbody = document.createElement("tbody")
table.appendChild(tbody)
var caption = table.createCaption()
caption.innerHTML = "学生成绩表"
var tr = tbody.insertRow (0)
var str = "学号,姓名,性别,语文,数学,英语".split(",")
for (var i = 0 i < str.length i++)
{
var th = document.createElement("th")
th.innerHTML = str[i]
tr.appendChild (th)
}
for (var i = 0 i < students.length i++)
{
var tr = tbody.insertRow (tbody.rows.length)
var obj = students[i]
for (var p in obj)
{
var op = obj[p]
if (p != "score")
{
var td = tr.insertCell (tr.cells.length)
td.innerHTML = op
}
else
{
for (var p in op)
{
var td = tr.insertCell (tr.cells.length)
td.innerHTML = op[p]
}
}
}
}
document.body.appendChild(table)
}
</script>
<body>
</body>
</html>
<!DOCTYPE HTML><html>
<head>
<title>Page Title</title>
<meta http-equiv="Content-Type" content="text/html charset=UTF-8" />
<style>
* {
margin: 0
padding: 0
}
.wrap {
margin: 20px auto
width: 600px
}
table {
width: 600px
border: 1px solid black
border-collapse: collapse
}
table input[type=button] {
width: 50px
height: 25px
border: 0
cursor: pointer
}
table input[type=button]:hover {
background-color: #fd3
}
table input[type=text] {
border-color: black
border-width: 0 0 1px 0
text-align: center
}
table textarea {
width: 90%
height: 100px
resize: none
}
th,td {
border: 1px solid black
text-align: center
}
</style>
<script>
var addRow = function(btn) {
var tbody = table.tBodies[0]
var row = btn.parentElement.parentElement.rowIndex
if (btn.value == '-') {
tbody.deleteRow(row)
} else {
var tr = tbody.insertRow(tbody.rows.length - 1)
var arr = [
"<select><option>城际交通费</option></select>",
"<input type='text' onkeyup='calc(this)' name='je' />",
"<input type='text' />",
"<input type='button' value='+' onclick='addRow(this)' />"
]
for (var i = 0 i < 4 i++) {
var td = tr.insertCell(tr.cells.length)
td.innerHTML = arr[i]
}
btn.value = '-'
}
}
var calc = function (txt){
if(!/^[+-]?((\d+(\.\d*)?)|(\d*\.\d+))$/.test(txt.value)) {
txt.value = txt.value.replace(/[^\+\-\.\d]/g, "")
}
var jes = document.getElementsByName("je")
var sum = 0
for(var i = 0 i < jes.length i++) {
sum += parseFloat(jes[i].value)
}
zje.value = sum
}
onload = function (){
dt.value = new Date().toLocaleString()
}
</script>
</head>
<body>
<div class="wrap">
<table id="table">
<tbody>
<tr>
<td>填写人:</td>
<td>
<input type="text" value="双q将" />
</td>
<td>填报时间:</td>
<td>
<input type="text" value="" id="dt" readonly />
</span>
</td>
</tr>
<tr>
<td>总金额:</td>
<td>¥
<input type="text" id="zje" readonly />
</td>
<td>状态:</td>
<td>
<input type="text" />
</td>
</tr>
<tr>
<th>项目</th>
<th>金额</th>
<th>费用说明</th>
<th> *** 作</th>
</tr>
<tr>
<td>
<select>
<option>城际交通费</option>
</select>
</td>
<td>
<input type="text" onkeyup="calc(this)" name="je" />
</td>
<td>
<input type="text" />
</td>
<td>
<input type="button" value="+" onclick="addRow(this)" />
</td>
</tr>
<tr>
<td>事由:</td>
<td colspan=3>
<textarea></textarea>
</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)