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++;
}
}
如何实现table所有td(可跨行跨列)的值存进二维数组
内容版权声明:除非注明,否则皆为本站原创文章。