javascript - jQuery 无法选择子项

标签 javascript jquery angularjs jquery-selectors

所以我使用 jQuery 在 Angular 后链接函数中选择一些元素。唯一的问题是,jQuery 找不到任何子项。

var tr = tbl.find('> thead > tr');

console.log('th', tr.children('th').length, tr.children().length, tr[0].children);

输出:

What I see in the console

什么给出了?

编辑

这就是 HTML 的本质

<table my-directive>
  <thead>
    <tr>
      <th sorting-header="foo">foo</th>
      <th sorting-header="foo">foo</th>
      <th sorting-header="foo">foo</th>
      <th sorting-header="foo">foo</th>
      <th sorting-header="foo">foo</th>
      <th sorting-header="foo">foo</th>
      <th sorting-header="foo">foo</th>
    </tr>
  </thead>
  ...
</table>

编辑2

我认为我遇到了某种类型的竞争状况。就像我说的,原始指令不是我写的,我只是对其进行了调整。似乎当我收到tbl时element 表中还没有元素(所以我想)因为 tr[0].innerHTML吐出一堆<!-- ngIf:子句但没有元素。我想这是一些令人毛骨悚然的竞争条件。

感谢大家的帮助,我想我现在要以不同的方式来解决这个问题。

最佳答案

很抱歉打扰大家,看起来这是一个经典的竞争条件。

我决定将其吐到控制台

console.log('th', tr.children('th').length, tr.children().length, tr[0].children.length)

现在我得到了 3 个零。所以我想我在控制台中看到的内容是由于控制台监视节点列表并实时更新其更改。

@Blazemonger 成功了。

关于javascript - jQuery 无法选择子项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26638826/

相关文章:

jQuery UI slider : how to inline it?

javascript - 当同级 ng-include 中的表单 $invalid 时,如何禁用 ng-include 中的按钮?

javascript - 索引数据库。如何在光标移动时更新记录

javascript - 在多个 Div Id 上使用 javascript

javascript - 组合 JavaScript 数组

javascript - Bootstrap 和 Angular ng-include 指令

javascript - angularjs - 我如何获得内容长度标题

JavaScript 复选框标签过滤器 : Issue to hide results when unchecked

javascript - 如何通过ajax在wordpress前端上传图片

javascript - 通过jquery获取复选框值