简体   繁体   English

jQuery找到上一个具有特定类单元格的表行

[英]jQuery find previous table row that has a cell with a specific class

Suppose I have a table like so: 假设我有一个像这样的表:

<table>
    <tr><td class="this-is-a-label">Label Cell</td></tr>
    <tr><td>Detail 1</td></tr>
    <tr><td class="selected">Detail 2</td></tr>
</table>

I want to be able to grab the previous "Label Cell" from the "Selected" cell. 我希望能够从“选定”单元格中获取先前的“标签单元格”。

My jQuery script should be something like: 我的jQuery脚本应类似于:

$('.selected').each(function() {
    var label = $(this).parent().prev('tr td.this-is-a-label');
    //... do what I need with the label ...
});

But it's not working. 但这不起作用。

Does anyone have any suggestions? 有没有人有什么建议?

You can do this: 你可以这样做:

$('.selected').each(function() {
  var label = $(this).closest('tr').prevAll('tr:has(td.this-is-a-label):first')
                     .children('td.this-is-a-label');
  //... do what I need with the label ...
});

This isn't ideal though, it's a rather expensive DOM traversal, if you can guarantee it's always 2 rows behind, you can do this: 但是,这并不是理想的选择,它是一个相当昂贵的DOM遍历,如果可以保证它总是落后2行,则可以执行以下操作:

$(this).closest('tr').prev().prev().children('td.this-is-a-label')

...which is much faster, it just depends what assumptions and guarantees you can make about your markup, if there are any certainties, you can definitely make it faster. ...速度要快得多,这仅取决于您可以对标记做出什么样的假设和保证,如果有任何确定性,则绝对可以使标记速度更快。

How about: 怎么样:

var label = 
  $('.selected').parent().prevAll('tr').children('td.this-is-a-label')[0];
$("td.selected").parents("table").find("td.this-is-a-label").text();

Here's how you get references to both the <tr> and <td> dom elements: 这是获取对<tr><td> dom元素的引用的方式:

$("tr:has(td.selected)").each(function(i, trSel){
    var trLabel = $(trSel).prevAll("tr:has(td.this-is-a-label)").get(0);

    var tdSel = $(trSel).children("td.selected").get(0);
    var tdLabel = $(trLabel).children("td.this-is-a-label").get(0);

    console.log(trSel, tdSel);
    console.log(trLabel, tdLabel);
});

I created a little plugin in response to this post for the purpose of finding a cell which is relative to the current cell: 为了响应此帖子,我创建了一个小插件,目的是查找相对于当前单元格的单元格:

$.fn.cellFromCell = function(r,c,upOrDown) {
    if(upOrDown == 'down') {
        return $(this).parent().prevAll(':eq(' + r + ')').find('td:eq(' + c + ')').text();
    } else {
        return $(this).parent().nextAll(':eq(' + r + ')').find('td:eq(' + c + ')').text();
    }
}

alert($('.selected').cellFromCell(1, 0, 'down')); // alerts "Label Cell"
alert($('.this-is-a-label').cellFromCell(0, 0)); // alerts "Detail 1"
​

I used your html for a simple test case. 我将您的html用于一个简单的测试用例。 You can mess with it here: http://jsfiddle.net/6kfVL/3/ 您可以在这里弄乱它: http : //jsfiddle.net/6kfVL/3/

You can make it easy by coding in an HTML5 id tag when generating the table: 您可以通过在生成表格时对HTML5 id标记进行编码来简化操作:

<table id="myTable">
    <tr id="label1"><td class="this-is-a-label">Label Cell</td></tr>
    <tr data-parent-label-id="label1"><td>Detail 1</td></tr>
    <tr data-parent-label-id="label1"><td class="selected">Detail 2</td>   </tr>
</table>

You can then use the value, and even perform actions on the associated "parent": 然后,您可以使用该值,甚至可以对关联的“父级”执行操作:

$("#myTable").on("click", "tr", function() {
    var associatedLabelId = $(this).data("parent-label-id");
    alert("Parent label of the row clicked: " + associatedLabelId);
    $("#" + associatedLabelId).addClass("highlight");
}

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

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