jQuery 在带有 rowspan 的表中选择可视列

发布于 2024-11-07 04:35:09 字数 895 浏览 1 评论 0原文

我见过一些类似的问题,但没有回答这个具体问题。考虑下表:

<table id="foo" border="1px">
    <tr>
        <td rowspan="2">one</td>
        <td>two</td>
        <td rowspan="2">three</td>
        <td>four</td>
        <td>five</td>
    </tr>
    <tr>
        <td rowspan="2">two</td>
        <td>four</td>
        <td>five</td>
    </tr>
    <tr>
        <td rowspan="2">one</td>
        <td>three</td>
        <td>four</td>
        <td>five</td>
    </tr>
    <tr>
        <td>two</td>
        <td>three</td>
        <td>four</td>
        <td>five</td>
    </tr>
</table>

使用 jQuery,如何选择特定可视列中的所有项目?例如,如果我想选择第 3 列,我应该获取所有以“三”为内容的 td。

I have seen a few similar questions but nothing that answers this specific problem. Consider the following table:

<table id="foo" border="1px">
    <tr>
        <td rowspan="2">one</td>
        <td>two</td>
        <td rowspan="2">three</td>
        <td>four</td>
        <td>five</td>
    </tr>
    <tr>
        <td rowspan="2">two</td>
        <td>four</td>
        <td>five</td>
    </tr>
    <tr>
        <td rowspan="2">one</td>
        <td>three</td>
        <td>four</td>
        <td>five</td>
    </tr>
    <tr>
        <td>two</td>
        <td>three</td>
        <td>four</td>
        <td>five</td>
    </tr>
</table>

Using jQuery, how can I select all items in a specific visual column? For example, if I want to select column 3, I should get all td's with 'three' as content.

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

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

发布评论

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

评论(5

度的依靠╰つ 2024-11-14 04:35:09

此插件处理复杂的列跨度和行跨度选择:

$('#foo th:nth-col(3), #foo td:nth-col(3)').css("color","red");

This plugin handles complex colspan and rowspan selection:

$('#foo th:nth-col(3), #foo td:nth-col(3)').css("color","red");
翻了热茶 2024-11-14 04:35:09

还没看过发布的插件,但我发现这个问题很有趣,所以我创建了一个小提琴!

http://jsfiddle.net/PBPSp/

如果插件有效,它可能会比这更好,但它是一个有趣的练习,所以我不妨将其发布。

colToGet 更改为您要突出显示的任何列。

$(function() {
    var colToGet = 2;

    var offsets = [];
    var skips = [];

    function incrementOffset(index) {
        if (offsets[index]) {
            offsets[index]++;
        } else {
            offsets[index] = 1;
        }
    }

    function getOffset(index) {
        return offsets[index] || 0;
    }

    $("#foo > tbody > tr").each(function(rowIndex) {

        var thisOffset = getOffset(rowIndex);

        $(this).children().each(function(tdIndex) {

            var rowspan = $(this).attr("rowspan");

            if (tdIndex + thisOffset >= colToGet) {
                if(skips[rowIndex]) return false;

                $(this).css("background-color", "red");

                if (rowspan > 1) {
                    for (var i = 1; i < rowspan; i++) {
                        skips[rowIndex + i] = true;
                    }
                }

                return false;
            }

            if (rowspan > 1) {
                for (var i = 1; i < rowspan; i++) {
                    incrementOffset(rowIndex + i);
                }
            }
        });
    });
});​

Haven't looked at the posted plugin, but I found the question interesting so I created a fiddle!

http://jsfiddle.net/PBPSp/

If the pluggin works it may be better than this, but it was a fun exercise so I may as well post it.

Change colToGet to whichever column you want to highlight.

$(function() {
    var colToGet = 2;

    var offsets = [];
    var skips = [];

    function incrementOffset(index) {
        if (offsets[index]) {
            offsets[index]++;
        } else {
            offsets[index] = 1;
        }
    }

    function getOffset(index) {
        return offsets[index] || 0;
    }

    $("#foo > tbody > tr").each(function(rowIndex) {

        var thisOffset = getOffset(rowIndex);

        $(this).children().each(function(tdIndex) {

            var rowspan = $(this).attr("rowspan");

            if (tdIndex + thisOffset >= colToGet) {
                if(skips[rowIndex]) return false;

                $(this).css("background-color", "red");

                if (rowspan > 1) {
                    for (var i = 1; i < rowspan; i++) {
                        skips[rowIndex + i] = true;
                    }
                }

                return false;
            }

            if (rowspan > 1) {
                for (var i = 1; i < rowspan; i++) {
                    incrementOffset(rowIndex + i);
                }
            }
        });
    });
});​
喜爱皱眉﹌ 2024-11-14 04:35:09

如果你想支持 colspan 和 rowspan,那么首先你需要
建表索引,即。识别每个细胞位置的矩阵
行,无论标记如何。然后你需要跟踪所有的事件
表格单元格并计算它们在矩阵和列中的偏移量
共享水平和垂直索引。

这是我拥有的一个插件 https://github.com/gajus/wholly 的描述为此目的而开发的。使用事件,您可以找到行或列中的所有值,包括使用 rowspan 或 solspan 属性附加的值。

我制作了一个可视化图,说明了表格和导航时触发的事件。

Wholly

橙色是活动单元格,红色是垂直事件触发的单元格,蓝色是水平事件触发的单元格。

If you want to support colspan and rowspan, then first you need to
build table index, ie. matrix that identifies cell position in every
row regardless of the markup. Then you need to track events of all the
table cells and calculate their offset in the matrix and the columns
that share the horizontal and vertical index.

This is the description of https://github.com/gajus/wholly, a plugin that I have developed for this purpose. Using the events you can find all the values in the row or in a col, including those attached using rowspan or solspan properties.

I made a visualisation illustrating a table and the events that are triggered upon navigation.

Wholly

Orange is the active cell, red are the cells triggered by the vertical event and blue are the cells triggered by the horizontal event.

妳是的陽光 2024-11-14 04:35:09

为每个行/列提供类。因此,第 1 行第 1 列将具有 class='Row1 Column1' 然后根据需要选择该类。 (如果您不想选择行,则不需要行规范,只是试图推断出如何进行网格。

Give each of your row / columns classes. So row 1 column 1 would have class='Row1 Column1' Then select on the class as needed. (If you do not want to ever select on rows you would not need the row specification just trying to extrapolate out how to do the grid.

独享拥抱 2024-11-14 04:35:09

我不确定你想如何选择它们,但是像这样的东西?

$(function() {
    $("#foo").find("td:contains('three')").css("background-color","#eee");
});

选择 TD 后您想对它们做什么?

I am not sure how you want to select them, but something like this?

$(function() {
    $("#foo").find("td:contains('three')").css("background-color","#eee");
});

What do you want to do with the TDs after you select them?

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