正确的 jQuery 选择器是什么?我想获取所有带有 attr x 的 tr,它们位于给定 tr 的正下方(同一级别)
我目前正在研究一棵树之类的东西,我希望能够折叠/展开孩子。我想最简单的方法是使用嵌套列表,但我无法使用它们,因为我无权访问 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您无法通过单个选择器来实现这一目标。这是我的方法: http://jsfiddle.net/RrnDG/1/ 请注意,我改变了将
id="fold"
更改为class="fold"
,因为多个 ID 具有相同值是无效的。 ID 应该是唯一的。我在这里所做的(基本上)是:
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"
toclass="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):
像这样的东西应该有效
something like this should work