在jquery中获取表的对角线和垂直相邻单元格

时间:2010-02-05 03:30:01

标签: javascript

我有一张4X4桌子。我希望得到相邻的垂直,并在底部的顶部对角线。当我点击的单元格在边缘时,我没有问题,因为我可以使用这样的东西。

above = $(that).parent().prev().children().first()
below = $(that).parent().prev().children().last()
diagonalLeft = $(that).parent().children().last().prev()
diagonalRight = $(that).parent().children().first().next()

但是当我遇到其中一种情况时,当我不能使用第一种或最后一种情况时,我不知道该怎么办。我无法弄清楚它的逻辑。

1 个答案:

答案 0 :(得分:0)

你只需要向后计算,告诉你目前在哪个栏目。

var $that = $('td.yourActiveTD')

    , rowNum = $that.parent().prevAll('tr').length
    , colNum = $that.prevAll('td').length

    , above = $that    // td
        .parent()      // tr
        .parent()      // table or tbody
        .children('tr')
        .eq(rowNum - 1) // the row above this one
        .children('td')
        .eq(colNum)    // in the same column as this
;

通过更改.eq()行,您应该能够获得所需的任何单元格。