在js中怎么获得动态增加的表格中某列的数据
2个回答
展开全部
<!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 = "学号,姓名,性别,语文,数学,<a href="https://www.baidu.com/s?wd=%E8%8B%B1%E8%AF%AD&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Ydm1bdPAcdmWPbPjDsuyw90ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EnH0YP1fzP1Rkn1ckrHTdrj6drf" target="_blank" class="baidu-highlight">英语</a>".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>
<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 = "学号,姓名,性别,语文,数学,<a href="https://www.baidu.com/s?wd=%E8%8B%B1%E8%AF%AD&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Ydm1bdPAcdmWPbPjDsuyw90ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EnH0YP1fzP1Rkn1ckrHTdrj6drf" target="_blank" class="baidu-highlight">英语</a>".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>
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2016-12-26
展开全部
确定获取的是第几列的数据
function getTdValue()
{
var tableId = document.getElementById("tab");
var str = "";
for(var i=1;i<tableId.rows.length;i++)
{
alert(tableId.rows[i].cells[1].innerHTML);
}
}
function getTdValue()
{
var tableId = document.getElementById("tab");
var str = "";
for(var i=1;i<tableId.rows.length;i++)
{
alert(tableId.rows[i].cells[1].innerHTML);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询