jQuery 需要根据 rowspan 循环下一个元素

发布于 2025-01-01 04:17:06 字数 2053 浏览 4 评论 0原文

好吧,基本上,我有一个像这样设置的表格,但是 元素可以有任意数量的行跨度...

<table width="100%">
    <tr class="tablerow0">
        <td class="tablecol_0" colspan="4">Content</td>
    </tr>
    <tr class="tablerow1">
        <td class="tablecol_0" rowspan="2">More Content</td>
        <td class="tablecol_1" colspan="3">Some More Content</td>
    </tr>
    <tr class="tablerow2">
        <td class="tablecol_1">And Yet More Content</td>
        <td class="tablecol_2">Content</td>
        <td class="tablecol_3">Content</td>
    </tr>
</table>

好吧,所以我想做的是确定行数rowspans 可以像这样完成:

$("td").each(function() {
    $(this).attr('rowspan');
});

但是对于每个大于 1 的 rowspans,我需要从每个函数中返回 false。例如,当它找到 rowspan 为 2 的元素时,它不应该继续到下一个元素,因为rowspan 附加到下一个元素。但更重要的是,我需要循环遍历 元素的行跨度。

例如,如果我有这个表设置:

<table width="100%">
    <tr class="tablerow0">
        <td class="tablecol_0" rowspan="2">Content</td>
        <td class="tablecol_1">Content</td>
        <td class="tablecol_2">Content</td>
    </tr>
    <tr class="tablerow1">
        <td class="tablecol_1">More Content</td>
        <td class="tablecol_2">Some More Content</td>
    </tr>
    <tr class="tablerow2">
        <td class="tablecol_1">And Yet More Content</td>
        <td class="tablecol_2">Content</td>
        <td class="tablecol_3">Content</td>
    </tr>
</table>

那么它需要使用 class="tablerow2" 获取这里的最后一个 因为 rowspan 仅覆盖 tablerow0tablerow1

希望您明白我的意思,它需要能够在 元素中循环,并且仅在超出这些 rowspan 数量时执行某些操作表中的 > 元素。行跨度可以超过 2,我只是对其进行简化,以便人们理解我在这里要问的内容。此外,可能有超过 1 个 元素的 rowspan 大于 1,因此它也应该能够知道这一点。我想我需要在这里构建一个 rowspan 数组,并且数组长度将基于每个 rowspan 值。

Well, basically, I have a table set up like so, but <td> elements can have any number of rowspans...

<table width="100%">
    <tr class="tablerow0">
        <td class="tablecol_0" colspan="4">Content</td>
    </tr>
    <tr class="tablerow1">
        <td class="tablecol_0" rowspan="2">More Content</td>
        <td class="tablecol_1" colspan="3">Some More Content</td>
    </tr>
    <tr class="tablerow2">
        <td class="tablecol_1">And Yet More Content</td>
        <td class="tablecol_2">Content</td>
        <td class="tablecol_3">Content</td>
    </tr>
</table>

Ok, so what I'd like to do is determine the number of rowspans which can be done like so:

$("td").each(function() {
    $(this).attr('rowspan');
});

But I need to return false out of this each function for each rowspan greater than 1. For example, when it finds the element with a rowspan of 2, it should not continue onto the next element, because the rowspan is attached to the next element. BUT MORE IMPORTANTLY, I need to loop through the rowspans for <tr> elements.

Example, if I have this table setup:

<table width="100%">
    <tr class="tablerow0">
        <td class="tablecol_0" rowspan="2">Content</td>
        <td class="tablecol_1">Content</td>
        <td class="tablecol_2">Content</td>
    </tr>
    <tr class="tablerow1">
        <td class="tablecol_1">More Content</td>
        <td class="tablecol_2">Some More Content</td>
    </tr>
    <tr class="tablerow2">
        <td class="tablecol_1">And Yet More Content</td>
        <td class="tablecol_2">Content</td>
        <td class="tablecol_3">Content</td>
    </tr>
</table>

Than it will need to get the last <tr> in here with class="tablerow2" because the rowspan only covers tablerow0 and tablerow1.

Hopefully you get what I mean, it needs to be able to loop within the <td> elements and only do something when the rowspan quantity has been exceeded for those <tr> elements in the table. The rowspan can be more than 2, I'm just simplifying this for people to grasp what I'm asking here. Also, there can be more than 1 <td> element with a rowspan greater than 1 so, it should be able to know this as well. I'm thinking I will need to build an array of rowspans here, and the array length will be based on each <td> rowspan value.

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

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

发布评论

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

评论(1

⊕婉儿 2025-01-08 04:17:07
//cache the tr elements to loop through them
var $TRs = $('tr');

//this type of loop will perform quickly
for (var i = 0, len = $TRs.length; i < len; i++) {

    //check to make sure the last TR element doesn't have a TD child element with a rowspan attribute
    if (i == 0 || $TRs.eq(i - 1).children('[rowspan]').length == 0) {
        //we are on the first TR element or the previous TR element does not have a child TD element with a rowspan attribute
    }
}

您还可以将 [rowspan] 更改为 [rowspan=2] 以检查特定的 rowspan 值。

这是一个演示: http://jsfiddle.net/JyQYW/

更新

这是一个简单的检查示例对于超过 rowspan=2

if (i == 0 || ($TRs.eq(i - 1).children('[rowspan=2]').length == 0 && (i > 1 && $TRs.eq(i - 2).children('[rowspan=3]').length == 0)) && (i > 2 && $TRs.eq(i - 3).children('[rowspan=4]').length == 0))) {

新代码会检查以确保向下有足够的行,然后检查最后三行是否影响当前行。

//cache the tr elements to loop through them
var $TRs = $('tr');

//this type of loop will perform quickly
for (var i = 0, len = $TRs.length; i < len; i++) {

    //check to make sure the last TR element doesn't have a TD child element with a rowspan attribute
    if (i == 0 || $TRs.eq(i - 1).children('[rowspan]').length == 0) {
        //we are on the first TR element or the previous TR element does not have a child TD element with a rowspan attribute
    }
}

You can also change [rowspan] to [rowspan=2] to check for a specific rowspan value.

Here is a demo: http://jsfiddle.net/JyQYW/

Update

Here is a simple example of checking for more than rowspan=2:

if (i == 0 || ($TRs.eq(i - 1).children('[rowspan=2]').length == 0 && (i > 1 && $TRs.eq(i - 2).children('[rowspan=3]').length == 0)) && (i > 2 && $TRs.eq(i - 3).children('[rowspan=4]').length == 0))) {

The new code checks to make sure we are enough rows down and then checks if the last three rows are affecting the current one.

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