在表中使用jQuery显示表外所选行的单个单元格内容

Using jQuery in table to display single cell contents of selected row outside of table

本文关键字:单个 单元格 jQuery 显示      更新时间:2023-09-26

这本质上是我在循环中的表:

<table id="tblMyTable">
  <tr>
     <td><%= attachment.Name %></td>
     <td><%= attachment.Description %></td>
     <td>
        <a id="clickPreview" href="#">Preview</a>
        <div id="divAttachmentContents" style="display:none;"> <%= attachment.ContentsAsHtml %>
        </div>
     </td>
   </tr>
</table>

从中我得到了多行数据。我想在最后一列的行的末尾有一个预览按钮,它将"预览"页面下方div中的内容。我认为我应该将内容呈现到一个隐藏的单元格中,以使其快速显示。

我需要知道如何告诉jQuery我所在的行,以及获取最后一个单元格的文本。

我需要的要点是:

    $('#clickPreview').click(function () {
        var newContent = $('#divAttachmentContents').text();
        $('#divAttachmentPreview').html(newContent);
    });

但这只适用于第一排。我可能不得不做一些类似的事情

<div id="<%= "divAttachmentContents_" + attachment.Id %>" style="display:none;"> <%= attachment.ContentsAsHtml %> </div>

但我不确定。这可能类似于这个问题。如有任何帮助,我们将不胜感激!

您可以为锚点分配一个类。这将使工作更容易。例如

$('.clickPreviewClass').click(function () {
    var newContent =$(this).next("div").text();
    $('#divAttachmentPreview').html(newContent);
});

假设clickPreviewClass是您提供锚点的类的名称。