获取类“b”的集合行类“a”之后的行直到下一个非“b”为止排?
我还不是一个 jQuery 选择器忍者,所以我问这个问题是希望有人能尝试一下。
我正在使用 jQuery 模板创建一个表,其中包含基于嵌套字典的行(它们嵌套了 5 层左右)。它基于有关组织结构和每个组中的人员的数据。根据下面的部分代码,我的目标是 - 对于每个“团队”行 - 获取属于该团队的“人员”行。这是我得到的内容,为了便于阅读而缩进。
<tr class="division">...</tr>
<tr class="team">...</tr>
<tr class="person">...</tr>
<tr class="person">...</tr>
<tr class="team">...</tr>
<tr class="person">...</tr>
<tr class="person">...</tr>
<tr class="person">...</tr>
<tr class="division">...</tr>
<tr class="team">...</tr>
我现在拥有的是一个 jQuery 选择器,它获取所有 class="team"
行,然后对于每一行,我尝试找到 选择器 来获取每个后续行class="person"
行,直到下一个非 person
行。我搞不懂。 是否需要用某些东西来代替 .nextAll()
方法?
$('table tr.team').each(function () {
var personRows = $(this).nextUntil('tr.team');
console.log('row count: ' + personRows.length);
});
我暂时登录到控制台,因此上面 HTML 的控制台输出应该匹配类似...
> row count: 2
> row count: 3
不幸的是,由于 “division”-类行。
> row count: 2
> row count: 4
我一直绞尽脑汁试图通过API来得到我想要的东西,但我无法得到想要的效果。有什么想法吗?有什么优雅的解决方案吗?我是否必须在某处添加一个 if
子句来确定该行是否是我想要包含在选择器中的行?
I'm not exactly a jQuery selector ninja yet, so I'm asking this in the hopes somebody can take a stab at it.
I'm using jQuery Templates to create a table with rows based on nested dictionaries (they're nested 5 layers deep or so). It's based on data regarding organizational structure, and the people in each group. What I aim to do, based on the partial code below, is -- for each "team" row -- get the row of "persons" belonging to that team. Here's what I've got, indented for ease-of-reading.
<tr class="division">...</tr>
<tr class="team">...</tr>
<tr class="person">...</tr>
<tr class="person">...</tr>
<tr class="team">...</tr>
<tr class="person">...</tr>
<tr class="person">...</tr>
<tr class="person">...</tr>
<tr class="division">...</tr>
<tr class="team">...</tr>
What I have now is a jQuery selector that gets all class="team"
rows, and then for each row, I'm trying to find the selector to get each subsequent class="person"
row, until the next non-person
row. I can't get it right. Does something need to go in the place of the .nextAll()
method?
$('table tr.team').each(function () {
var personRows = $(this).nextUntil('tr.team');
console.log('row count: ' + personRows.length);
});
I'm logging to the console for the time being so console output for the HTML above should match something like...
> row count: 2
> row count: 3
Unfortunately, I'm getting something more along the lines of this, because of that "division"
-class row.
> row count: 2
> row count: 4
I've been racking my brain trying to look through the API to get what I want, but I can't get the desired effect. Any ideas? Any elegant solutions? Am I going to have to throw in an if
clause somewhere to determine if the row is one I want to include in my selector?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我想我刚刚想明白了。我尝试了很长时间来解决这个问题,但直到我发布了一个问题之后才解决......你知道这是怎么回事。
无论哪种情况,
.filter()
都是我的朋友。Okay, I suppose I just figured it out. I tried for a long time to figure it out and couldn't until after I posted a question... you know how that goes.
In either case,
.filter()
is my friend.