jQuery:选择表的区域

时间:2012-11-11 14:03:03

标签: jquery select html-table

我有一个场景,我希望一次从列表中选择2 td的区域

我有一张这样的表:

<tr>
    <td>R1:C1</td>
    <td>R1:C2</td>
</tr>
<tr>
    <td>R2:C1</td>
    <td>R2:C2</td>
</tr>
<tr>
    <td>R3:C1</td>
    <td>R3:C2</td>
</tr>
<tr>
    <td>R4:C1</td>
    <td>R4:C2</td>
</tr>

我希望例如选择单元格R1:C1R2:C1是否将鼠标悬停在其中一个上,当我将鼠标悬停在R3:C1R4:C1上时,它会选择单元格R3:C1R4:C1

基本上,它会威胁一个包含4行2列的表,就像一张只有2行和2列的表

编辑:我想像一个一样威胁一组x个单元格,以便我可以在其上应用事件监听器

1 个答案:

答案 0 :(得分:1)

您可以使用类来选择正确的单元格,例如

<tr>
<td class="group1">R1:C1</td>
<td>R1:C2</td>
</tr>
<tr>
<td class="group1">R2:C1</td>
<td>R2:C2</td>
</tr>
<tr>
<td class="group2">R3:C1</td>
<td>R3:C2</td>
</tr>
<tr>
<td class="group2">R4:C1</td>
<td>R4:C2</td>
</tr>

jQuery改变悬停背景

$('.group1').hover(
    function(){$('.group1').css('background-color','black');}, 
    function(){$('.group1').css('background-color','white');});
$('.group2').hover(
    function(){$('.group2').css('background-color','black');}, 
    function(){$('.group2').css('background-color','white');});

希望有所帮助