正确的 jQuery 选择器是什么?我想获取所有带有 attr x 的 tr,它们位于给定 tr 的正下方(同一级别)

发布于 2024-09-19 03:56:31 字数 1186 浏览 3 评论 0原文

我目前正在研究一棵树之类的东西,我希望能够折叠/展开孩子。我想最简单的方法是使用嵌套列表,但我无法使用它们,因为我无权访问 HTML 代码。这是相关的简化 HTML 代码:

<table>
    <tbody>
        <tr rel="1"><td><a href="#" id="fold">fold</a> item 1</td></tr>
        <tr rel="2"><td><a href="#" id="fold">fold</a> item 1.1</td></tr>
        <tr rel="2"><td><a href="#" id="fold">fold</a> item 1.2</td></tr>
        <tr rel="1"><td>item 2</td></tr>
        <tr rel="1"><td><a href="#" id="fold">fold</a> item 3</td></tr>
        <tr rel="2"><td><a href="#" id="fold">fold</a> item 3.1</td></tr>
        <tr rel="3"><td>item 3.1.1</td></tr>
        <tr rel="3"><td>item 3.1.2</td></tr>
</tbody>
</table>

如您所见,没有嵌套项目,但有可用的“级别”,所以我想应该可以实现我的想法。折叠/展开部分正在工作,当我单击第 1 项上的折叠时,rel >= 2 的项目将被隐藏。但这不是我想要的;我只想折叠 rel 高于我单击折叠的项目,并希望隐藏所有这些项目,直到到达 rel 低于或等于当前 rel 的元素。

例如,当我单击第 1 项中的折叠时,第 1.1 项和第 1.2 项应隐藏,但第 3.1 项应保持可见。

有没有人可以帮助我开始?谢谢。

比约恩

I'm currently working on something like a tree, and I want to be able to fold/unfold children. I guess the most easy way to get this is by using nested lists, but I can't use them because I don't have access to the HTML code. This is the relevant simplified HTML code:

<table>
    <tbody>
        <tr rel="1"><td><a href="#" id="fold">fold</a> item 1</td></tr>
        <tr rel="2"><td><a href="#" id="fold">fold</a> item 1.1</td></tr>
        <tr rel="2"><td><a href="#" id="fold">fold</a> item 1.2</td></tr>
        <tr rel="1"><td>item 2</td></tr>
        <tr rel="1"><td><a href="#" id="fold">fold</a> item 3</td></tr>
        <tr rel="2"><td><a href="#" id="fold">fold</a> item 3.1</td></tr>
        <tr rel="3"><td>item 3.1.1</td></tr>
        <tr rel="3"><td>item 3.1.2</td></tr>
</tbody>
</table>

As you can see, no nested items, but there are 'levels' available, so I guess it should be possible to do what I have in mind. The fold/unfold part is working, f.e. when I click fold on item 1, items with rel >= 2 are hidden. But that's not what I want; I only want to fold the items which rel is higher than the one where I clicked fold, and want to hide all of them until I reach an element which rel is lower or equal to the current rel.

For example, when I click fold in item 1, item 1.1 and 1.2 should be hidden, but item 3.1 should stay visible.

Is there anyone that can help me getting started? Thanks.

Bjorn

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

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

发布评论

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

评论(2

从﹋此江山别 2024-09-26 03:56:31

我认为您无法通过单个选择器来实现这一目标。这是我的方法: http://jsfiddle.net/RrnDG/1/ 请注意,我改变了将 id="fold" 更改为 class="fold",因为多个 ID 具有相同值是无效的。 ID 应该是唯一的。

我在这里所做的(基本上)是:

  • 获取单击行的级别
  • 遍历所有后续行。对于每个做:
    • 检查级别是否更高(例如更深的嵌套);如果为 true,则切换可见性,否则中止遍历

I do not think that you can achieve this with a single selector. This is my approach: http://jsfiddle.net/RrnDG/1/ Note that i changed your id="fold" to class="fold", since having multiple IDs with the same value is invalid. An ID is meant to be unique.

What i am doing here is (basically):

  • get level of the clicked row
  • traverse all following rows. for each do:
    • check if level is higher (e.g. deeper nested); if true, toggle visibility, otherwise abort traversing
流殇 2024-09-26 03:56:31

像这样的东西应该有效

$("#fold").click(function()
{
    var _current = $(this).parent().parent();
    var _level = _current.attr('rel');

    _current.nextAll().each(function()
    {
        if($(this).attr('rel') > _level){
            $(this).hide();
        }else{
            // Nested children stopped here, we don't continue
            return false;
        }
    });        
});

something like this should work

$("#fold").click(function()
{
    var _current = $(this).parent().parent();
    var _level = _current.attr('rel');

    _current.nextAll().each(function()
    {
        if($(this).attr('rel') > _level){
            $(this).hide();
        }else{
            // Nested children stopped here, we don't continue
            return false;
        }
    });        
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文