使用 Jquery 将 div 与相同计数器链接

发布于 2024-10-04 21:19:34 字数 506 浏览 0 评论 0原文

我有一个使用 PHP 从数据库获取数据的表。

该表包含父级 2 个元素,假设它们是:

Name    Surname    << table row id = info1

Name    Surname    << table row id = info2

在每一行下面,还有默认隐藏的另一行。单击名字时,这就是我希望它显示的方式:

Name    Surname    << table row id = info1
DOB     City       << table row id = infoSub1

Name    Surname    << table row id = info2
                   << table row id = infoSub2 NOT DISPLAYED

如何以最有效的方式实现这一点?

谢谢

I have a table which gets data from the database using PHP.

The table contains the parent 2 elements, lets say they are:

Name    Surname    << table row id = info1

Name    Surname    << table row id = info2

Beneath every row, there is another row which is hidden by default. When first name is clicked, this is how i want it to display:

Name    Surname    << table row id = info1
DOB     City       << table row id = infoSub1

Name    Surname    << table row id = info2
                   << table row id = infoSub2 NOT DISPLAYED

How can I implement this in the most effective way?

Thanks

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

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

发布评论

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

评论(2

む无字情书 2024-10-11 21:19:34
$("#info1").click(function() {
    $(this).children("#infoSub1").slideDown();
});

然后对 info2 和 infoSub2 执行相同的操作:)

$("#info1").click(function() {
    $(this).children("#infoSub1").slideDown();
});

Then do the same for info2 and infoSub2 :)

灼痛 2024-10-11 21:19:34

如果您可以为父行指定一个类(例如 parent),那么您可以使用 .nextUntil()

$("tr.parent").click(function() {
  $(this).nextUntil(".parent").toggle();
});

这仅选择所有元素,不是 .parent 跟随单击的行,直到找到该行。这允许您在中间插入任意数量的非父行,并且可以正确切换它们。如果您有一个包含很多行的长表,请使用 .delegate() 以类似的方式:

$("#tableID").delegate("tr.parent", "click", function() {
  $(this).nextUntil(".parent").toggle();
});

其行为相同,但会将一个事件处理程序附加到 (而不是每个父级 )。

If you can give the parent rows a class (for example parent), then you can make this very generic with .nextUntil():

$("tr.parent").click(function() {
  $(this).nextUntil(".parent").toggle();
});

This just selects all the <tr> elements that aren't .parent following the clicked row until it finds one that is. This allows you any number of non-parent rows in-between, and it would toggle them all correctly. If you have a long table with lots of rows, use .delegate() in a similar way:

$("#tableID").delegate("tr.parent", "click", function() {
  $(this).nextUntil(".parent").toggle();
});

This behaves the same, but would attach one event handler to the <table> (rather than one per parent <tr>).

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