2016-11-28 2 views
0

Ich habe den folgenden Code, aber ich kann nicht sagen, warum es Spalten auf verschiedenen Tabellen versteckt. Es scheint, dass es den ersten Teil des Selektors ignoriert, der die Tabellen-ID ist.jQuery Verstecken von Spalten in verschiedenen Tabellen

<html> 
<head> 
    <script src="https://code.jquery.com/jquery-1.10.2.js"></script> 
</head> 
<body> 
    <table id="table1"> 
     <thead> 
      <tr> 
       <th>Col1</th> 
       <th>Col2</th> 
       <th>Col3</th> 
       <th>Col4</th> 
       <th>Col5</th> 
       <th>Col6</th> 
       <th>Col7</th> 
      </tr> 
     </thead> 
     <tbody> 
      <tr> 
       <td>Val1</td> 
       <td>Val2</td> 
       <td>Val3</td> 
       <td>Val4</td> 
       <td>Val5</td> 
       <td>Val6</td> 
       <td>Val7</td> 
      </tr> 
     </tbody> 
    </table> 
    <table id="table2"> 
     <thead> 
      <tr> 
       <th>Col1</th> 
       <th>Col2</th> 
       <th>Col3</th> 
       <th>Col4</th> 
       <th>Col5</th> 
       <th>Col6</th> 
       <th>Col7</th> 
      </tr> 
     </thead> 
     <tbody> 
      <tr> 
       <td>Val1</td> 
       <td>Val2</td> 
       <td>Val3</td> 
       <td>Val4</td> 
       <td>Val5</td> 
       <td>Val6</td> 
       <td>Val7</td> 
      </tr> 
     </tbody> 
    </table> 
    <table id="table3"> 
     <thead> 
      <tr> 
       <th>Col1</th> 
       <th>Col2</th> 
       <th>Col3</th> 
       <th>Col4</th> 
       <th>Col5</th> 
       <th>Col6</th> 
       <th>Col7</th> 
      </tr> 
     </thead> 
     <tbody> 
      <tr> 
       <td>Val1</td> 
       <td>Val2</td> 
       <td>Val3</td> 
       <td>Val4</td> 
       <td>Val5</td> 
       <td>Val6</td> 
       <td>Val7</td> 
      </tr> 
     </tbody> 
    </table> 
<script> 
    $(document).ready(function() { 
     $('#table1 td:nth-child(4),th:nth-child(4)').hide(); 
    }); 
</script> 
</body> 

habe ich versucht, diese Lösung zuerst https://stackoverflow.com/a/5901376/3658485

Bin ich die Tabelle auszuwählen gehen zu müssen, und seine th/tds iterieren?

Antwort

2

Sie sollten dies setzen:

'#table1 td:nth-child(4), #table1 th:nth-child(4)' 
+0

Yep, das es war. Ich musste die Tabellen-ID nach dem Komma wieder einfügen. Das macht total Sinn. Vielen Dank. –

1
$(document).ready(function() { 
    $('#table1 td:nth-child(4),th:nth-child(4)').hide(); 
}); 

Ihr Code ist die Auswahl des td: n-Kind (4) der Tabelle 1, aber es ist auch die Auswahl der th: n-Kind (4) von allen Tabellen.

beste Lösung ist die td rufen/th Sie versteckte wollen und es auf den Tisch geben, wie so:

$(document).ready(function() { 
    $('td:nth-child(4),th:nth-child(4)', '#table1').hide(); 
}); 
Verwandte Themen