所以我使用 jQuery 在 Angular 后链接函数中选择一些元素。唯一的问题是,jQuery 找不到任何子项。
var tr = tbl.find('> thead > tr');
console.log('th', tr.children('th').length, tr.children().length, tr[0].children);
输出:
什么给出了?
编辑
这就是 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/