如何根据其位置引用表格单元格? [英] How can I reference a table cell by its position?

查看:29
本文介绍了如何根据其位置引用表格单元格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表格中有一行,行的头部和96个单元格。

I have a row in a table with the head of the row and 96 cells.

每行都可以通过以下函数点击

Each row is clickable by the following function

$("#table tr").click(function(){});

现在通过从0到96获取值,我需要得到 td 在地方X并在其上执行 colspan

Now by getting value from 0 to 96, I need to get the td in the place X and do a colspan on it.

在表格中,我得到的值40我需要在点击的行中的第40个 td 上设置 colspan

As in the form, I get value 40 I need to make the colspan on the 40th td in the clicked row.

如何在JavaScript或jQuery中实现类似的东西?

How can I implement something like that in JavaScript or jQuery?

感谢Pete评论我已经有了如何获得 td 更改属性,现在问题是如果我 colspan 表格中的单元格在屏幕上变形

Thank's to Pete comment I've got how to get td to which change the attribute, the problem now is if I colspan the cell the table become deformed as on the screen

< img src =https://i.stack.imgur.com/GNc0R.pngalt =在此处输入图像说明>

推荐答案

你可以尝试这样的事情。您需要先添加colspan属性,然后需要删除最后一个单元格,如下所示:

You can try something like this. You need to add colspan attribute first and then need to remove last cells as below:

            $(document).ready(function(){            
                $("#tables tr").click(function() {
                    let position = 2;
                    let colspan = 2;

                    $(`td:eq(${position-1})`, this).prop('colspan', colspan);
                    $(`td:eq(${position+colspan})`, this).nextAll().remove();

                });
            });

希望它可以帮到你;)

这篇关于如何根据其位置引用表格单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆