遍历html表格中的隐藏列

发布于 2024-10-31 15:21:12 字数 1086 浏览 0 评论 0原文

也许这个问题很简单...我对 html、css、dom 和 javascript 理解得很好,但是在尝试让 jQuery 为我工作时我遇到了非常困难的时期。假设我有以下 4 列表,其中第 1 列和第 3 列隐藏:

<table id="mytable">
  <thead>  
    <tr> 
      <th class="hidden">Column1</th> 
      <th>Column2</th>    
      <th class="hidden">Column3</th> 
      <th>Column4</th>
    </tr>
  </thead>
  <tbody> 
    <tr> 
      <td class="hidden">Value1</td> 
      <td>Isle of Palms</td>   
      <td class="hidden">Value3</td>  
      <td>Value4</td> 
    </tr> 
  </tbody>
</table>      

然后我使用以下代码隐藏隐藏的类列:

$(function() { 
    $('.hidden').hide();
});             

昨天你们中的一些人告诉我如何使用以下方式获取第一列内容:

$(function() { 
    $(this).find('td.hidden:first').html();
});             

我想要什么要做的就是显示一个警报或厚盒,显示所有隐藏列的标题名称和单元格值:

Column1 = Value1
Column3 = Value3

如果更容易做到,我可以将所有隐藏列分组在表的开头(左侧)。

提前致谢。

Maybe this question is so easy... I understand html, css, the dom and javascript pretty good, but I'm having a very tough time while trying to get jQuery to work for me. Let's say I have the following 4 columns table, with columns 1 and 3 hidden:

<table id="mytable">
  <thead>  
    <tr> 
      <th class="hidden">Column1</th> 
      <th>Column2</th>    
      <th class="hidden">Column3</th> 
      <th>Column4</th>
    </tr>
  </thead>
  <tbody> 
    <tr> 
      <td class="hidden">Value1</td> 
      <td>Isle of Palms</td>   
      <td class="hidden">Value3</td>  
      <td>Value4</td> 
    </tr> 
  </tbody>
</table>      

Then I use the following code to hide the hidden class columns:

$(function() { 
    $('.hidden').hide();
});             

Yesterday some of you told me how to get the first column content by using:

$(function() { 
    $(this).find('td.hidden:first').html();
});             

What I want to do is to show an alert or Thickbox showing the header name and cell value for all the hidden columns:

Column1 = Value1
Column3 = Value3

If it were easier to do, I could group all the hidden columns at the beginning (to the left) of the table.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

美人迟暮 2024-11-07 15:21:12

试试这个

var mytable = $('#mytable');

$('#mytable > tbody').delegate('tr', 'click', function (e) {

    trToShow = $(this);

    var keyValueInfo = mytable.find('th.hidden').map(function () {

        // for each th.hidden element, get the corresponding "key = value" string

        var i = $(this).index(); // get the index of the th element

        // get the td element with the same index as the above th, and get the text inside it
        var value = trToShow.find('td.hidden').filter(function () {
            return $(this).index() === i;
        }).text();

        // The above could also be done as (Read documentation of :eq selector)
        // var value = trToShow.find('td:eq(' + i + ')').text();

        // get the text inside this th element, which would be our key
        var key = $(this).text();

        // return the "key = value" string wrapped up in a div element
        return '<div>' + key + ' = ' + value + '</div>';

    }).toArray().join('');

});

// Show a dialog with the above content inside it
show_dialog_with_stuff(keyValueInfo);

index 上阅读更多内容

我还没有测试过,但如果有的话,应该只会有一些轻微的愚蠢错误。在这里测试http://jsfiddle.net /mb6Gd/

Try this out

var mytable = $('#mytable');

$('#mytable > tbody').delegate('tr', 'click', function (e) {

    trToShow = $(this);

    var keyValueInfo = mytable.find('th.hidden').map(function () {

        // for each th.hidden element, get the corresponding "key = value" string

        var i = $(this).index(); // get the index of the th element

        // get the td element with the same index as the above th, and get the text inside it
        var value = trToShow.find('td.hidden').filter(function () {
            return $(this).index() === i;
        }).text();

        // The above could also be done as (Read documentation of :eq selector)
        // var value = trToShow.find('td:eq(' + i + ')').text();

        // get the text inside this th element, which would be our key
        var key = $(this).text();

        // return the "key = value" string wrapped up in a div element
        return '<div>' + key + ' = ' + value + '</div>';

    }).toArray().join('');

});

// Show a dialog with the above content inside it
show_dialog_with_stuff(keyValueInfo);

Read more up on index

I haven't tested it, but if any, there should only be minor silly errors. Tested here http://jsfiddle.net/mb6Gd/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文