使用 jquery find() 查找表元素的直接 tr 元素
我试图找到表元素内的所有 tr 元素。表元素本身具有嵌套在其行内的其他表元素。因此,当我执行以下操作时:
$(tbl).find('tr').hover(...);
...它也会拾取嵌套表元素内的 tr 元素。我只想要我尝试查询的表元素的直接 tr 元素。
$(tbl).find('>tr').hover(...);
对我不起作用!
ps: tbl
是表格元素,而不是字符串
I am trying to find all tr elements inside a table element. The table element it self has other table elements nested inside its rows. So when I do the following:
$(tbl).find('tr').hover(...);
...it picks up tr elements inside the nested table elements also. I just want the immediate tr elements of the table element I am trying to query.
$(tbl).find('>tr').hover(...);
didn't work for me!
ps: tbl
is a table element, not a string
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
请阅读完整答案。
将为您提供
tbody
和thead
(如果存在)。所以如果你想要立即 tr,你需要尝试这个。
$(tbl).children('tbody').children('tr')
在获取
table
的子项时,不要忘记考虑tbody
,就像在 Firefox 中一样直接调用$(tbl).children()
等子级会返回tbody
而不是tr
,即使它不在您的标记中。复杂但有用。快乐编码。
Read full answer, please.
will give you
tbody
and alsothead
if it exists.So if you want immediate tr, you will need to try this.
$(tbl).children('tbody').children('tr')
Don't forget to consider
tbody
while fetching children of atable
, as in Firefox a direct call to children like$(tbl).children()
returnstbody
and nottr
, even if its not in your markup. Complex but useful.Happy Coding.
这应该有效:
形成 JQuery docu:
.find() 和 .children() 方法是类似,只不过后者仅在 DOM 树中向下移动一层。
This should work:
Form the JQuery docu:
The .find() and .children() methods are similar, except that the latter only travels a single level down the DOM tree.
不要忘记 - 即使您的标记不包含它们 - 从 DOM 的角度来看, tr 元素嵌套在 tbody 元素内,因此您需要将其包含在选择器中。
尝试类似...
虽然这可能也会找到嵌套表的 tbody,所以您可能需要类似...
Don't forget that - even if your markup doesn't include them - from a DOM point of view, tr elements are nested inside a tbody element, so you will need to include this in the selector.
Try something like...
Although this will probably find the nested table's tbody too, so you'll probably need something like...