我有一个有多个td rowspan的表.鼠标悬停时,整个字母行应为红色.例如,如果我们将鼠标放在任何字母值上,则整个字母表部分应显示为红色.数字的情况也是如此.
有一些jQuery实现这一点,但无法得到相同颜色的整行字母或数字.试试这个:http://jsfiddle.net/y3q2bs85/6/
HTML代码:
<table>
<tbody>
<tr>
<td rowspan="3">Alphabet</td>
<td rowspan="2">a</td>
<td>b</td>
<td>c</td>
</tr>
<tr>
<td>d</td>
<td>e</td>
</tr>
<tr>
<td>f</td>
<td>g</td>
<td>h</td>
</tr>
<tr>
<td rowspan="3">Number</td>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>4</td>
<td>5</td>
<td>6</td>
</tr>
<tr>
<td>7</td>
<td>8</td>
<td>9</td>
</tr>
</tbody>
</table>
Run Code Online (Sandbox Code Playgroud)
CSS代码:
body {
padding: 50px;
}
table {
width: 100%;
border-collapse: collapse;
}
td, th {
padding: 20px;
border: 1px solid black;
}
.hover {
background: red;
}
Run Code Online (Sandbox Code Playgroud)
jQuery代码:
$("td").hover(function() {
$el = $(this);
$el.parent().addClass("hover");
if ($el.parent().has('td[rowspan]').length == 0)
$el
.parent()
.prevAll('tr:has(td[rowspan]):first')
.find('td[rowspan]')
.addClass("hover");
}, function() {
$el
.parent()
.removeClass("hover")
.prevAll('tr:has(td[rowspan]):first')
.find('td[rowspan]')
.removeClass("hover");
});
Run Code Online (Sandbox Code Playgroud)
我们怎么解决这个问题?
编辑:添加了一种查找每个块顶部的方法.
编辑2 - 事先做好工作再考虑一下这个问题,最好只在每个块的行中处理并在每个块中存储该列表,例如每个字母行存储对包含行的数组的引用1-4.因此,当您悬停时,您只需要获取存储在父行中的行数组并将类应用于这些行.
通过检查块顶行中的最大行距,您不仅可以检查第一个单元格.在更新的代码中,我已将Alphabet移到中间以演示此内容并添加几个其他块来演示单行块的工作原理.
function findBlocks(theTable) {
if ($(theTable).data('hasblockrows') == null) {
console.log('findBlocks'); // to prove we only run this once
// we will loop through the rows but skip the ones not in a block
var rows = $(theTable).find('tr');
for (var i = 0; i < rows.length;) {
var firstRow = rows[i];
// find max rowspan in this row - this represents the size of the block
var maxRowspan = 1;
$(firstRow).find('td').each(function () {
var attr = parseInt($(this).attr('rowspan') || '1', 10)
if (attr > maxRowspan) maxRowspan = attr;
});
// set to the index in rows we want to go up to
maxRowspan += i;
// build up an array and store with each row in this block
// this is still memory-efficient, as we are just storing a pointer to the same array
// ... which is also nice becuase we can build the array up in the same loop
var blockRows = [];
for (; i < maxRowspan; i++) {
$(rows[i]).data('blockrows', blockRows);
blockRows.push(rows[i]);
}
// i is now the start of the next block
}
// set data against table so we know it has been inited (for if we call it in the hover event)
$(theTable).data('hasblockrows', 1);
}
}
$("td").hover(function () {
$el = $(this);
//findBlocks($el.closest('table')); // you can call it here or onload as below
$.each($el.parent().data('blockrows'), function () {
$(this).find('td').addClass('hover');
});
}, function () {
$el = $(this);
$.each($el.parent().data('blockrows'), function () {
$(this).find('td').removeClass('hover');
});
});
findBlocks($('table'));Run Code Online (Sandbox Code Playgroud)
body {
padding: 50px;
}
table {
width: 100%;
border-collapse: collapse;
}
td, th {
padding: 20px;
border: 1px solid black;
}
.hover {
background: red;
}Run Code Online (Sandbox Code Playgroud)
<table>
<tbody>
<tr>
<td>Symbols</td>
<td>+</td>
<td>-</td>
<td>*</td>
</tr>
<tr>
<td rowspan="2">a</td>
<td>b</td>
<td rowspan="4">Alphabet</td>
<td>c</td>
</tr>
<tr>
<td>d</td>
<td>e</td>
</tr>
<tr>
<td rowspan="2">f</td>
<td>g</td>
<td>h</td>
</tr>
<tr>
<td>i</td>
<td>j</td>
</tr>
<tr>
<td>Bitwise</td>
<td>&</td>
<td>|</td>
<td>^</td>
</tr>
<tr>
<td rowspan="3">Number</td>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>4</td>
<td>5</td>
<td>6</td>
</tr>
<tr>
<td>7</td>
<td>8</td>
<td>9</td>
</tr>
</tbody>
</table>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>Run Code Online (Sandbox Code Playgroud)