我试图从我的表 strip 中排除嵌套表(使每隔一行具有不同的 bg 颜色)。这是我对表格进行条纹的代码:

$(".stripeTable tbody tr:odd").addClass("stripe");

我的问题是,如何防止嵌套表的奇数行接收“ strip ”类?

这是浏览器生成的代码,我想从嵌套表中删除 class="stripe"。
   <table>
      <tr>
        <td>My Table Cell </td>
      </tr>
      <tr class="stripe">
        <td>
          <table>
            <tr>
              <td>My nested table cell</td>
            </tr>
            <tr class="stripe">
              <td>my nested table cell (remove the stripe!)</td>
            </tr>
          </table>
        </td>
      </tr>
    </table>

最佳答案

如果只有顶级表有 stripeTable 类,只需添加一些子选择器 > :

$(".stripeTable > tbody > tr:odd").addClass("stripe");

如果嵌套表也有 stripeTable 类,您可能需要使用另一个子选择器将 .stripeTable anchor 定到另一个父元素:
$(".parent > .stripeTable > tbody > tr:odd").addClass("stripe");

关于jquery - 使用 jQuery 从表 strip 化中排除嵌套表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7275167/

10-12 04:00