如何通过 jQuery 函数仅获取直接子元素

发布于 2024-09-18 06:43:58 字数 868 浏览 3 评论 0原文

我有一个像这样的表结构:

<table1>
  <tbody>
    <tr>
      <td></td>
        ...
      <td>
        <table2>
          <tbody>
            <tr>
              <td></td>
            </tr>
          </tbody>
        </table>
      </td>
    </tr>
   </tbody>
  </table>

在javascript中,我有一个变量tbl,其值为$(table1),然后我想获取所有直接子元素(tr ) table1。 我的代码是:

$('tr', tb1)

显然它返回 table1 和 table2 中的所有 元素。 我想我可以通过

$('tr', tb1).not(function(){return $(this).parent().parent()[0] != tb1;})

这种逻辑。

我知道 $('table1 > tbody > tr') 可以获取直接子 tr。不幸的是我不能使用这个。

有人对此有什么好主意吗?

谢谢。

I have a table structure like this:

<table1>
  <tbody>
    <tr>
      <td></td>
        ...
      <td>
        <table2>
          <tbody>
            <tr>
              <td></td>
            </tr>
          </tbody>
        </table>
      </td>
    </tr>
   </tbody>
  </table>

In javascript, I have a variable tbl with value of $(table1), and then I want to get all direct child elements (tr) of <tbody> of table1.
My code is :

$('tr', tb1)

Apparently it returns all <tr> elements in table1 and table2.
I think I can get by

$('tr', tb1).not(function(){return $(this).parent().parent()[0] != tb1;})

or this kind of logic.

I know $('table1 > tbody > tr') can get the direct child tr. Unfortunately I can not use this.

Anyone has good idea about this?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

日久见人心 2024-09-25 06:44:08

如果您有两个元素的 id 并且想要查找直接元素,请使用下面的代码

$("#parent > #two")

如果您想要嵌套搜索,则可以使用 find。
这里有详细的解释。
https://handyopinion.com/jquery-selector-find-nested-child -元素/

If you have ids of both elements and you want to find direct element use below code

$("#parent > #two")

If you want a nested search you can use find.
It is explained in detail here.
https://handyopinion.com/jquery-selector-find-nested-child-elements/

-小熊_ 2024-09-25 06:44:05

这正是人们应该小心使用嵌套表的原因。我真的希望您将它们用于数据而不是页面布局。

另一个可能会毁了你一天的问题是在嵌套表上使用 CSS 选择器...你基本上有同样的问题 - 如果不选择内部表中的 TR 元素,你就无法选择外部表的 TR 元素。 (您不能使用子选择器,因为 IE6 中未实现它)

这应该可行:

$("#table1 > tbody > tr")

但是,我建议您对 TBODY 元素进行硬编码,因为您不应该依赖浏览器为您创建它。

This is exactly the reason why one should be careful with nesting tables. I really hope that you use them for data and not page layout.

Another issue that will probably ruin your day is using CSS selectors on nested tables... you basically have the same issue - you are unable to select TR elements of the outer table without selecting those inside the inner table, too. (You cannot use the child selector because it is not implemented in IE6)

This should work:

$("#table1 > tbody > tr")

However, I recommend that you hardcode the TBODY element, since you should not rely on the browser to create it for you.

安静被遗忘 2024-09-25 06:44:04

正如 @jave.web 在评论中提到的,

要搜索元素的直接子元素,请使用 .children()。它只会搜索直接子级,而不会进行更深层次的遍历。 http://api.jquery.com/children/

As @jave.web mentioned in the comments

To search through the direct children of an element use .children(). It will only search through the direct children and not traverse any deeper. http://api.jquery.com/children/

玩世 2024-09-25 06:44:02

您可以使用 find():

tbl.find(" > 正文 > ”)

You can use find():

tbl.find("> tbody > tr")

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文