简体   繁体   English

使用 JQuery 查找不在 header 中且仅在表的第一列中的单元格

[英]Find cells that are not in the header and are only in the first column of a table using JQuery

I used to have something like,我曾经有过类似的东西,

$(elem).parents('li').find(...)

Where elem was an item in a list, so it was easy to get a reference to all of the items in the list.其中 elem 是列表中的一个项目,因此很容易获得对列表中所有项目的引用。 Now however I have added more information and decided to use a table, where the list fits into the table as follows.然而,现在我添加了更多信息并决定使用一个表格,该列表适合如下表格。

[header][header][header] [标题][标题][标题]

[list 1][ cell ][ cell ] [列表 1][ 单元格 ][ 单元格 ]

[list 2][ cell ][ cell ] [列表 2][ 单元格 ][ 单元格 ]

[list 3][ cell ][ cell ] [列表 3][ 单元格 ][ 单元格 ]

I'm a little stuck creating the equivalent JQuery do a.find() on just the cells that have the list items in it.我有点难以创建等效的 JQuery 仅在其中包含列表项的单元格上执行 a.find() 。 The list items are always in the left-most table cells excluding the header.列表项始终位于最左侧的表格单元格中,不包括 header。

Here is what the table looks like in html.这是 html 中的表格。

<table id="my-table">
<tr>
<th>Column 1</th>
<th>Column 2</th>
<th>Column 3</th>
<th>Column 4</th>
</tr>
<tr>
<td>list item 1</td>
<td>junk</td>
<td>junk</td>
<td>junk</td>
</tr>
<tr>
<td>list item 2</td>
<td>junk</td>
<td>junk</td>
<td>junk</td>
</tr>
</table>

If the li's are always in the first cell of each row then this should work:如果 li 总是在每行的第一个单元格中,那么这应该有效:

$('td:first-child>li')

Use:利用:

$(elem).closest('tr').find(...)

If you make use of thead and tbody you can find rows only in the body much easier.如果你使用theadtbody ,你可以更容易地在 body 中找到行。

Change your markup to something like this:将您的标记更改为如下所示:

<table>
    <thead>
       ... header rows ...
    </thead>
    <tbody>
       ... body rows ...
    </tbody>
</table>

Then you can simply include tbody in your jquery selector to find just rows which are body rows.然后,您可以简单地将tbody包含在您的 jquery 选择器中,以仅查找作为主体行的行。

Something like #my-table tbody td:first-child .#my-table tbody td:first-child类的东西。 Where first-child will get you the first column. first-child将在哪里为您提供第一列。

This will give you only the first column in each row.这将只为您提供每行中的第一列。

var rows = $('tr :nth-child(1)', '#my-table').not('th');

If you want to loop through and do something to each of these now, just use:如果你现在想循环并做一些事情,只需使用:

rows.each(function()
{
   //Do something with the columnn
});

The solution below will output the matching elements, first <td> in each row, to <div id="#results"下面的解决方案将 output 匹配元素,每行中的第一个<td> ,到<div id="#results"

Working example at: http://jsfiddle.net/6faUf/工作示例: http://jsfiddle.net/6faUf/

HTML: HTML:

<table border="5">
    <thead>
        <tr><th>1</th><th>2</th><th>3</th></tr>
    </thead>
    <tbody>
        <tr><td><ul><li>List1</li></ul></td><td>2</td><td>3</td></tr>
        <tr><td><ul><li>List2</li></ul></td><td>2</td><td>3</td></tr>
    </tbody>
</table>
<div id="results">The list values are: </div>

JavaScript (jQuery): JavaScript(jQuery):

$('td:first-child').each(function(){
    var value = $(this).text();
    $("#results").append(value);
});

If you need the cells that have the list items in them, you'd need the :has() selector, so there'd be something like that:如果您需要其中包含列表项的单元格,则需要:has()选择器,所以会有类似的东西:

  • $(elem).closest('table').find('td:has(li)...') — if you need all the li in the table $(elem).closest('table').find('td:has(li)...') — 如果您需要表中的所有li
  • or $(elem).closest('tr').find('td:has(li)...') — if you need all the li in the raw$(elem).closest('tr').find('td:has(li)...') - 如果您需要原始中的所有li

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

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