JavaScript使用函数创建表格
<button onclick="formStuden()">点我创建表格</button>
<script>
function formStuden() {
var atb = document.createElement('table');
document.body.appendChild(atb);
var os = document.createElement('caption');
atb.appendChild(os);
os.innerHTML = ('学生信息表');
var ot = document.createElement('thead');
atb.appendChild(ot);
var or = document.createElement('tr');
ot.appendChild(or);
var th1 = document.createElement('th');
th1.innerHTML = ('编号');
or.appendChild(th1);
var th2 = document.createElement('th');
th2.innerHTML = ('姓名');
or.appendChild(th2);
var th3 = document.createElement('th');
th3.innerHTML = ('年龄');
or.appendChild(th3);
var th4 = document.createElement('th');
th4.innerHTML = ('操作');
or.appendChild(th4);
var op = document.createElement('tbody');
atb.appendChild(op);
for (i = 0; i < 10; i++) {
var rr = document.createElement('tr');
op.appendChild(rr);
var oth1 = document.createElement('th');
rr.appendChild(oth1);
oth1.innerHTML = ('1000');
var oth2 = document.createElement('th');
rr.appendChild(oth2);
oth2.innerHTML = ('张三');
var oth3 = document.createElement('th');
rr.appendChild(oth3);
oth3.innerHTML = ('20');
var oth4 = document.createElement('th');
rr.appendChild(oth4);
var och = document.createElement('a');
och.innerHTML = ('删除');
och.onclick = function () {
var s = this.parentNode.parentNode;
s.parentNode.removeChild(s);
}
oth4.appendChild(och);
}
}
</script>