如何获取 td 的列 id(而不是 td 的列号)?
在此示例中:
<table border="1">
<col id="col0" style="background-color: #FFFF00"/>
<col id="col1" style="background-color: #FF0000"/>
<tr><td rowspan="2">1</td><td>2</td><td>3</td></tr>
<tr><td>4</td><td>5</td><td>6</td></tr>
<tr><td>7</td><td>8</td><td>9</td></tr>
</table>
如何获取 td 4 的 col id?
如果我用这个 jquery 命令得到它的列号:
var cn = $(this).parent().children().index($(this));
cn 将为 0,但它的样式显示它属于 col1 当我在 td 上方的 td 处设置 rowspan="2" 时,我需要像 td.col.id 这样的推荐
(例如 td 4),此 td 的列号将与其 col(或 colgroup)的顺序不同,并且我设置背景颜色来显示它。
编辑: 我相信有一种方法可以解决这个问题,因为当 td 知道它是 col(colgroup) 时,必须有一种方法可以在 dom 树上向 td 询问它。 (Td4你展示了特定col的风格,那个col是谁?)
In this example:
<table border="1">
<col id="col0" style="background-color: #FFFF00"/>
<col id="col1" style="background-color: #FF0000"/>
<tr><td rowspan="2">1</td><td>2</td><td>3</td></tr>
<tr><td>4</td><td>5</td><td>6</td></tr>
<tr><td>7</td><td>8</td><td>9</td></tr>
</table>
How can I get the col’s id of td 4?
If I get it's column number with this jquery command:
var cn = $(this).parent().children().index($(this));
cn will be 0, but it’s style shows that it belongs to col1
and I need a commend like td.col.id
when I set rowspan="2" at the td above a td (eg. td 4) this td's column number will be different from it's order of col(or colgroup) and I set background color to show it.
Edit:
I believe there is a way to solve this problem, because when td knows about it's col(colgroup) there must be a way to ask it from td at dom tree. (Td4 you show style of a specific col, who is that col?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
4
是第二个表行的第一个子级,因此您确实应该获得第 0 列。与其详细说明检测行跨度等的复杂函数,不如仅为每个表格单元格分配 id,或为您的表格创建另一个自定义解决方案。
例如,您事先知道每个特定行有多少列?或者您使用实际的背景颜色或“秘密”CSS 属性作为标识。
附:我无用的小提琴直到我理解了实际的问题。
编辑(阅读下面的讨论):
如此处所述,你不应该创建自定义 css 属性;这些通常会被浏览器忽略(并且无法通过
.attr()
获得)。萨哈尔的解决方案是标记受行合并影响的每个元素,以记住该元素应计算多少列。
<td>4</td>
is the first child of the second tablerow, so you should indeed get column 0.instead of elaborating a complex function that detects rowspans etc, it might be advisable to just assign ids to each table cell, or create another custom solution for your table.
e.g. you know in advance how many columns each specific row has? Or you use the actual background color or a 'secret' css attribute as identification.
ps. my useless fiddle until I understood the actual problem.
edit (read discussion below):
as described here, you are not supposed to create custom css attributes; these are often ignored by the browser (and not available via
.attr()
).Sahar's solution was to mark each element affected by a merging of rows to remember for how many columns the element should count.
您首先必须计算 td 本身的列号。
这是通过计算 td 之前的 td 数量来完成的;考虑 colspan 属性:
然后您可以迭代
col
元素并返回与列号匹配的元素。我们首先必须找到 colgroup 元素。然后它类似于计算 td 的列号:
通过这两个函数,您应该能够执行此操作:
http: //jsfiddle.net/wHyUQ/1/
jQuery 版本
http://jsfiddle.net/wHyUQ/2/
You first have to calculate the column number of the td itself.
This is done by counting the number of tds before our td; taking colspan attributes into account:
Then you can iterate the
col
elements and return the one matching the column number.We first have to find the colgroup element. Then it's similar to computing the column number of the td:
With these two function you should be able to do this:
http://jsfiddle.net/wHyUQ/1/
jQuery version
http://jsfiddle.net/wHyUQ/2/
解决行跨度或列跨度将非常复杂。我建议您迭代所有
col
元素,将它们的宽度设置为0px
并检查这是否影响您的td
或第元素。如果是这样,这是相关的专栏。
例子:
Resolving rowspans or colspans would be incredibly complex. I suggest you to iterate over all
col
-elements, set a width of0px
to them and check if this affected the width of yourtd
orth
element. If so, this is the related column.Example: