【发布时间】:2018-09-01 06:35:52
【问题描述】:
我使用的答案是:
How can I find each table cell's "visual location" using jQuery?
但在这种情况下似乎不起作用:http://jsfiddle.net/TRr6C/9/
注意 1,3 1,4 和 2,4 应该是 1,4 1,5 和 2,5
有人知道有什么问题吗?
或者考虑到 colspan 和 rowspan,有没有更好的解决方案来从表格单元格中获取 cellIndex 和 rowIndex?
代码如下:
function getCellLocation(cell) {
var cols = cell.closest("tr").children("td").index(cell);
var rows = cell.closest("tbody").children("tr").index(cell.closest("tr"));
var coltemp = cols;
var rowtemp = rows;
cell.prevAll("td").each(function() {
cols += ($(this).attr("colspan")) ? parseInt($(this).attr("colspan")) - 1 : 0;
});
cell.parent("tr").prevAll("tr").each(function() {
//get row index for search cells
var rowindex = cell.closest("tbody").children("tr").index($(this));
// assign the row to a variable for later use
var row = $(this);
row.children("td").each(function() {
// fetch all cells of this row
var colindex = row.children("td").index($(this));
//check if this cell comes before our cell
if (cell.offset().left > $(this).offset().left) {
// check if it has both rowspan and colspan, because the single ones are handled before
var colspn = parseInt($(this).attr("colspan"));
var rowspn = parseInt($(this).attr("rowspan"));
if (colspn && rowspn) {
if(rowindex + rowspn > rows)
cols += colspn;
}
}
});
});
return {
rows: rows,
cols: cols
};
}
【问题讨论】:
-
按照你的逻辑,1,3 也是错误的,对吧?应该是 1,4。
-
@Hemlock 是的,应该是 1,4
-
@Petah 问题在于当该单元格位于前一行时,不考虑 colspan 大于 1 的“td”。通过获取 cellIndex 和 rowIndex 你想做什么?我也许能够提供一个解决方案。这里是jQuery complex table parser,您将找到技术文档Table Usability Concept 并使用示例HTML Tables and WET Table Parser :-)
-
这不是 colspan 它是 rowspan 抱歉。因此,问题在于当该单元格位于前一行时,不考虑行跨度大于 1 的“td”。 :-)
标签: javascript jquery html-table