如何实现table所有td(可跨行跨列)的值存进二维数组

var rows = 0, cols = 0, nums = 0;var trs = document.getElementsByTagName("tr");var row = document.getElementsByTagName("tr")[0];var tds = row.getElementsByTagName("td");for (var i...
var rows = 0, cols = 0, nums = 0;
var trs = document.getElementsByTagName("tr");
var row = document.getElementsByTagName("tr")[0];
var tds = row.getElementsByTagName("td");
for (var i = 0; i < tds.length; i++) {
if (parseInt(tds[i].getAttribute("colspan")) > 0)
cols += parseInt(tds[i].getAttribute("colspan"));
else
cols++;
}
for (var i = 0; i < trs.length; i++) {
var tds = trs[i].getElementsByTagName("td");
for (var j = 0; j < tds.length; j++) {
var rowspan = parseInt(tds[j].getAttribute("rowspan"));
var colspan = parseInt(tds[j].getAttribute("colspan"));
if (rowspan > 0 && colspan > 0)
nums += rowspan * colspan;
else {
if (rowspan > 0)
nums += rowspan;
else if (colspan > 0)
nums += colspan;
else 
nums++;
}
}
}
rows = nums / cols;
var arr = new Array(rows);
var flag = new Array(rows);
for (var i = 0; i < rows; i++) {
arr[i] = new Array(cols);
for (var j = 0; j < cols; j++) 
arr[i][j] = 0;
}
for (var i = 0; i < rows; i++) {
flag[i] = new Array(cols);
for (var j = 0; j < cols; j++) 
flag[i][j] = 0;
}
for (var i = 0; i < trs.length; i++) {
var tds = trs[i].getElementsByTagName("td");
for (var j = 0; j < tds.length; j++) {
var rowspan = parseInt(tds[j].getAttribute("rowspan"));
var colspan = parseInt(tds[j].getAttribute("colspan"));
for (var p = 0; flag[i][p] == 1; p++);
if (colspan > 0 && rowspan > 0) {
for (var m = 0; m < rowspan; m++) {
for (var n = 0; n < colspan; n++) {
arr[i + m][p + n] = tds[j].firstChild.nodeValue;
flag[i + m][p + n] = 1;
}
}
}
else {
if (colspan > 0) {
for (var k = 0; k < colspan; k++) {
arr[i][p + k] = tds[j].firstChild.nodeValue;
flag[i][p + k] = 1;
}
}
else {
arr[i][p] = tds[j].firstChild.nodeValue;
flag[i][p] = 1;
}
if (rowspan > 0) {
for (var k = 0; k < rowspan; k++) {
arr[i + k][p] = tds[j].firstChild.nodeValue;
flag[i + k][p] = 1;
}
}
else {
arr[i][p] = tds[j].firstChild.nodeValue;
flag[i][p] = 1;
}
}
p++;
}
}

你可能感兴趣的文章

相关问题

0 条评论

请先 登录 后评论
廖雪
廖雪

78 篇文章

作家榜 »

  1. admin 651 文章
  2. 粪斗 185 文章
  3. 王凯 92 文章
  4. 廖雪 78 文章
  5. 牟雪峰 12 文章
  6. 李沁雪 9 文章
  7. 全易 2 文章
  8. Garmcrypto7undop 0 文章