【问题标题】:jQuery: Selecting elements that don't have a specific descendant elementjQuery:选择没有特定后代元素的元素
【发布时间】:2009-10-21 16:48:19
【问题描述】:

如何使用 jQuery 1.3.2 选择所有没有任何后代 td 元素的表格元素?

【问题讨论】:

    标签: jquery jquery-selectors


    【解决方案1】:

    你可以试试:

    $("table:not(:has(tbody > tr > td))").doStuff();
    

    工作示例:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    <head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
    <script type="text/javascript">
    $(function() {
      $("table:not(:has(tbody > tr > td))").css("background", "yellow");
    });
    </script>
    <style type="text/css">
    table { border-collapse: collapse; }
    td, th { border: 1px solid black; }
    </style>
    </head>
    <body>
    <table>
    <tr>
      <td>First table</td>
    </tr>
    </table>
    <table>
    <tr>
      <th>Second table</th>
    </tr>
    </table>
    </body>
    </html>
    

    【讨论】:

      【解决方案2】:

      您正在寻找CSS :not() selector

      table *:not(td)
      

      应该这样做。

      编辑:呸,误读了你想要的。

      【讨论】:

      • 在正确的轨道上,但tables 通常永远不会拥有tds 的直系子级,因此这不会像所写的那样工作。
      • 是的,忍者隐身编辑。我认为他正在寻找表格本身,而不是我认为他正在寻找的 tr/td/th。
      • 是的,我自己在找桌子。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-21
      • 1970-01-01
      相关资源
      最近更新 更多