我有一个表,用户可以用鼠标选择行和列,然后将选定的区域合并到一个单元格中,它的工作原理就像ms单词表的行为一样。html表格选择与colspan或rowspan
但是,当表具有rowSpan或colSpan时,所选区域不是常规矩形,所以我想将选择扩展为常规矩形以便稍后合并。
这里是rowspan和colspan的例子,当选择不包括td有rowspan时,它工作正常,否则,选择是错误的。当你有colspan > 1
,因为小区更换后的cellIndex
值的细胞来调整由前一小区采取的colspan
细胞
$('td').mousedown(function() {
$(this).closest('table').find('td').removeClass('selected');
var start = {
x: this.cellIndex,
y: this.parentNode.rowIndex
}
$(this).closest('table').find('td').mouseover(function() {
var x1 = Math.min(start.x, this.cellIndex);
var y1 = Math.min(start.y, this.parentNode.rowIndex);
var x2 = Math.max(start.x, this.cellIndex);
var y2 = Math.max(start.y, this.parentNode.rowIndex);
$(this).closest('table').find('td').each(function() {
var x = this.cellIndex;
var y = this.parentNode.rowIndex;
if (x >= x1 && x <= x2 && y >= y1 && y <= y2) {
$(this).addClass('selected');
} else {
$(this).removeClass('selected');
}
});
});
var self = this;
$(document).mouseup(function() {
$(self).closest('table').find('td').unbind('mouseover');
$(document).unbind('mouseup');
});
});
复杂的问题...我现在不会尝试: –
不错的问题。正如RaviH所指出的那样,这里的问题是当单元格中有colspan/rowspan的单元格时,单元格的'cellIndex'属性会给出误导性的值。见http://stackoverflow.com/questions/10966687/how-can-i-find-each-table-cells-visual-location-using-jquery/10967488#10967488 – tewathia