0

すべての [tr] からいくつかの情報を含む 2 番目の [td] を取得しようとすると、ロジックに行き詰まります。ここにいくつかの例があります。

<table class="todos">
 <tbody>
   <tr>
      <td>info1</td>
      <td>info2</td>
      <td>info3</td>
    </tr>
  </tbody>
</table>

そして、これが私のためにそれを見つけるためのJavascriptです...

$(".todos tbody tr").css('display','none');
$(".todos tbody tr").each(function(){
    if($(this).find("td:eq(0):contains("+procurar+")"))
        $(this).css('display','table-row');
    if($(this).find("td:eq(1):contains("+procurar+")"))
        $(this).css('display','table-row');
});
4

4 に答える 4

0

If you want to get the [tr]s that have at least 2 [td]s containing some information you should use :contains before :lt.

HTML

<table class="todos">
    <tbody>
        <tr>
            <td>info1</td>
            <td>info2</td>
            <td>info3</td>
        </tr>
        <tr>
            <td>info1</td>
            <td>other</td>
            <td>info3</td>
        </tr>
        <tr>
            <td>info1</td>
            <td>other</td>
            <td>some other</td>
        </tr>
    </tbody>
</table>

JS:

var procurar = 'info';
$(".todos tr").css('display', 'none');
$(".todos tbody tr").each(function () {
    if ($(this).find('td:contains("' + procurar + '"):lt(2)').length > 1) {
        $(this).css('display', 'table-row');
    }
});

Result:

info1   info2   info3
info1   other   info3

http://jsfiddle.net/4Sj7t

Else, if you want to get the [tr]s whose 2nd [td] contains some information you should use :lt before :contains, as in your example.

HTML

<table class="todos">
<tbody>
    <tr>
        <td>info1</td>
        <td>info2</td>
        <td>other</td>
    </tr>
    <tr>
        <td>info1</td>
        <td>other</td>
        <td>info2</td>
    </tr>
</tbody>

JS:

var procurar = 'info';
$(".todos tr").css('display', 'none');
$(".todos tbody tr").each(function () {
    if ($(this).find('td:lt(2):contains("' + procurar + '")').length > 1) {
        $(this).css('display', 'table-row');
    }
});

Result:

info1   info2   other

http://jsfiddle.net/4Sj7t/1/

于 2013-04-01T15:15:55.387 に答える
0

親を非tr表示にしているため、に別の表示プロパティを設定しても表示されtdませんが、それが意図した機能でない場合は、次のようにします。

$(".todos tbody tr").hide().find('td:lt(2)').filter(function() {
    return $(this).text().indexOf(procurar) != -1;
}).closest('tr').css('display','table-row');
于 2013-04-01T14:37:51.027 に答える