简体   繁体   English

如何从下一行获取与当前TD相同索引的TD

[英]How to get TD from next row with same index as current TD

I have a large HTML table where all rows of the body have the same structure. 我有一个很大的HTML表,其中正文的所有行都具有相同的结构。

Within this table there are editable TDs (which have the class " editable " and contain a contenteditable div) and non-editable TDs (which dont't have the class " editable " and do not contain a div). 在此表中,有可编辑的TD(类别为“ editable ”,并且包含一个contenteditable div)和不可编辑的TD(不具有类别“ editable ”,并且不包含div)。

Now I am trying to get the TD from the next row that has the same index as the current (closest) TD . 现在,我试图从下一行获取与当前(最接近)TD索引相同的TD

The below code gives me the correct index of my current TD within its row (and looking only at editable TDs). 下面的代码为我提供了当前TD在其行内的正确索引(并且仅查看可编辑的TD)。

Can someone tell me how I can get the equivalent TD of the next row ? 有人可以告诉我如何获得下一行的等效TD吗?

My jQuery: 我的jQuery:

$(document).keydown(function(e) {
    var current = $(e.target);
    var editables = $(current).closest('tr').find('td.editable');
    var count = editables.length;
    alert( editables.index($(current).closest('td')) ); // for testing
    // ...
});

Instead of the alert I am looking for something like the following: 我正在寻找类似以下内容的警报:

$(current).closest('tr').next('tr').find( /* the td with class editable AND the index matching the above */ );

Example: 例:
If I am currently on the 4th editable TD in a row I would then need the 4th editable TD in the next row. 如果我当前连续处于第4个可编辑TD,则在下一行中需要第4个可编辑TD。

Try using :eq() like 尝试像这样使用:eq()

$(document).keydown(function(e){
    var current = $(e.target);
    var editables = current.closest('tr').find('td.editable');
    var count = editables.length;
    var index = editables.index(current.closest('td')); 
    current.closest('tr').next('tr').find('td:eq('+index+')');
});

As commented above, you can use current instead of $(current) 如上所述,您可以使用current代替$(current)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM