如果你想这样做,而不使用选择,你可以这样做:
function getNextCellVertically(htmlCell){
//find position of this cell..
var $row = $(htmlCell).parent();
var cellIndex = $.inArray(htmlCell, $row[0].cells);
var table = $row.parent()[0];
var rowIndex = $.inArray($row[0], table.rows);
//get the next cell vertically..
return (rowIndex < table.rows.length-1) ?
table.rows[rowIndex+1].cells[cellIndex] : undefined;
}
$('td').click(function() {
var nextCell = getNextCellVertically(htmlCell);
//...
});
不是说效率是这里重要的,但它工作得更快做这样的 - 超过10万次迭代它在测试中比基于选择器的方法快2-5倍。
你不需要''td''参数给孩子,如果OP在表中有['th' elements](http://jsfiddle.net/nX7JP/),可能会抛出它。 – Dennis 2012-01-12 18:23:20