2013-04-17 71 views
1

更TR我有jQuery代码:如何选择HTML表格

$('td').hover(function() { 
    var t = $(this), 
      index = t.index(); // the position of the TD in the row, so you can find the one below it 
    t.addClass('hovered'); // apply hovered class to this TD... 
    t.nextAll(':lt(3)').addClass('hovered'); // ...and to the TD next to it... 
    t.closest('tr') 
    .nextAll(':lt(3)') 
    .find('td:eq(' + index + ')') 
    .addClass('hovered') 
    .nextAll(':lt(3)') 
    .addClass('hovered'); // ...and to the 2 below 
}, function() { 
    // remove the hovered class when no longer hovering 
    $(this).closest('table').find('td').removeClass('hovered'); 
}); 

JSFiddle DEMO

如何做出什么选择4x4的细胞?不是2x4 +2像演示。

+0

你应该看看tableHover插件,这真是太棒了:http://p.sohei.org/stuff/jquery/tablehover/demo/demo.html – mattytommo

回答

1

尝试

$('td').hover(function() { 
    var t = $(this), 
     index = t.index(); // the position of the TD in the row, so you can find the one below it 
    t.addClass('hovered'); // apply hovered class to this TD... 
    t.nextAll(':lt(3)').addClass('hovered'); // ...and to the TD next to it... 
    t.closest('tr').nextAll(':lt(3)').each(function(i,v){ 
     $('td:eq(' + index + ')', this).addClass('hovered').nextAll(':lt(3)').addClass('hovered'); // ...and to the 2 below 
    }) 
}, function() { 
    // remove the hovered class when no longer hovering 
    $(this).closest('table').find('td').removeClass('hovered'); 
}); 

演示:Fiddle

更简化版本

$('td').hover(function() { 
    var t = $(this), 
     index = t.index(); // the position of the TD in the row, so you can find the one below it 

    var trs = t.closest('tr').nextAll(':lt(3)').addBack(); 
    trs.find('td:eq(' + index + ')').add(trs.find('td:gt(' + (index) + '):lt(3)')).addClass('hovered'); 
}, function() { 
    // remove the hovered class when no longer hovering 
    $(this).closest('table').find('td').removeClass('hovered'); 
}); 

演示:Fiddle

+0

很好。谢谢。 – user2289809

1
$('td').hover(function() { 
    var t = $(this), 
     index = t.index(); // the position of the TD in the row, so you can find the one below it 
    t.addClass('hovered'); // apply hovered class to this TD... 
    t.nextAll(':lt(1)').addClass('hovered'); // ...and to the TD next to it... 
    t.closest('tr').nextAll(':lt(1)').find('td:eq(' + index + ')').addClass('hovered').nextAll(':lt(1)').addClass('hovered'); // ...and to the 2 below 
}, function() { 
    // remove the hovered class when no longer hovering 
    $(this).closest('table').find('td').removeClass('hovered'); 
}); 

我将lt(3)更改为lt(1)。看看这是你想要的。

看到的结果是:http://jsfiddle.net/FXy5J/3/

+0

如果我改变lt(1)将是2x2 – user2289809

+0

我误解了,我的不好。查询已解决,祝您好运! – AnAspiringCanadian

0
var selectY = 4, selectX = 4 

$('td').hover(function() {  
    var $cell = $(this), 
    $table = $cell.parents("table"), 
    x = $cell.index(), 
    y = $cell.parent().index(); 

    $table.find("tr:eq("+y+"),tr:gt("+y+"):lt("+(selectY-1)+")") 
     .find("tr:eq("+x+"),td:gt("+x+"):lt("+(selectX-1)+")") 
    .addClass("hovered"); 
}, function(){ 
    $(this).parents("table") 
    .find("td") 
    .removeClass("hovered");  
}); 

您可以用selectX选择您所在地区和Y瓦尔太

http://jsfiddle.net/PKahK/2/

*更新,因为GT似乎并没有被接受-1:/