【问题标题】:I need a better jQuery selector to cut down on children() calls我需要一个更好的 jQuery 选择器来减少 children() 调用
【发布时间】:2011-05-12 10:59:45
【问题描述】:

目前我正在使用 $('table').children('tfoot').children('tr').children('td');

获取 tfoot 中唯一的 td。

我不喜欢使用.children() 3 次,

有没有更好的办法?


编辑
轻微修正 选择器实际上是
var table = this;
$(table).children('tfoot').children('tr').children('td');

因为这是在一个 jquery 插件中。

【问题讨论】:

    标签: javascript jquery jquery-selectors


    【解决方案1】:
    $('table>tfoot td');
    

    您最好阅读有关 CSS 样式选择器的信息。

    对于更新版本:

    $('tfoot>td',this);
    

    【讨论】:

      【解决方案2】:
      $('table > tfoot > tr > td')
      

      children() 搜索直系子女,因此为了复制这一点,我使用了 direct descendant selector (>)。

      更新

      根据您的更新,您可以...

      $(table).find(' > tfoot > tr > td')
      

      或者您可以将table 替换为this

      很高兴你是thinking of the children

      【讨论】:

      • 干杯,请看我上面的编辑,我试过$(table).children('tfoot > tr > td');但这没有用,有什么想法吗?
      • @Hailwood 这应该可以,因为table 应该只有一个tfoot(即不是多个也不是嵌套的)。我们能看到一些 HTML 吗?
      • @Hailwood 很长 5 分钟 :P
      • @alex 说我的时钟停止了修复它;)
      • 甚至$("tfoot > tr > td", table);
      猜你喜欢
      • 2014-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多