标题几乎说明了一切。结果 赋予相同的表

<table>
  <tr>
    <th>Header 1</td>
    <th>Header 2</td>
  </tr>
  <tr>
    <td>Datum 1</td>
    <td> Datum 2</td>
  </tr>
  <tr>
    <td>Datum 1</td>
    <td> Datum 2</td>
  </tr>
</table>

如何,选择包含有第标头和行没有其他?

有帮助吗?

解决方案

表达式:

$(function() {
  $("tr:has(th):not(:has(td))").hide();
});

甚至只是:

$(function() {
  $("tr:not(:has(td))").hide();
});

完整的示例:

<html>
<head>
  <title>Layout</title>
</head>
<body>
<table>
  <tr>
    <th>Header 1</td>
    <th>Header 2</td>
  </tr>
  <tr>
    <td>Datum 1</td>
    <td> Datum 2</td>
  </tr>
  <tr>
    <td>Datum 1</td>
    <td> Datum 2</td>
  </tr>
  <tr>
    <th>Header 3</th>
    <th>Datum 2</td>
  </tr>
</table>
<script src="http://www.google.com/jsapi"></script>
<script>
  google.load("jquery", "1.3.1");
  google.setOnLoadCallback(function() {
    $(function() {
      $("tr:has(th):not(:has(td))").hide();
    });
  });
</script>
</body>
</html>

其他提示

如果可能的话,这将是理想的,以包围元素中的表头

<table>
    <thead>
        <tr>
            <th></th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td></td>
        </tr>
    </tbody>
</table>

这样一来,选择你的表头将是简单:

$('thead');

或者仅选择

$('thead tr');
然后

您的标记将是更加可读,并且样式表的变得更容易,因为可以更容易地定位在表格的头部或身体的元件。

jQuery("tr:has(th)")

则选择包含每tr至少一个 th

kthxbye:)

使用:has选择器:

$('tr:has(th)').addClass('sample')

如果您有混合thtd儿童表虽然可能不会工作。

说不上,如果它像道场CSS3的查询,但你可以使用第一子类伪,如果jQuery的支持的话:

TR个:第一胎

TR日:第n个孩子(1)

请参阅: http://www.456bereastreet.com/archive/200601/css_3_selectors_explained/

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top