为给定表格行中的每个单元格设置 CSS 类的有效方法是什么?
我在为给定表格行中的每个单元格设置 CSS 类时遇到问题。最初我认为设置父行CSS会影响单元格的样式属性, 但这行不通。相反,我必须循环遍历给定行中的所有单元格来更新 CSS 类。
然而,这效率不高。这花了很多时间。考虑我的情况:我有大约 230 行,其中每行有 23 个单元格(总共 5290 个单元格)。
注意:我不使用任何框架。那么您能建议一种原生 JS 方法吗?
更新:
使用Paolo的建议它工作正常。
最初我的自定义CSS类是这样的
.Grid_RowItalicsBold { PADDING-RIGHT: 5px; PADDING-LEFT: 8px; FONT-WEIGHT: bold; FONT-SIZE: 8pt; OVERFLOW: hidden; COLOR: Black; LINE-HEIGHT: 15pt; FONT-STYLE: normal; FONT-FAMILY: Verdana, Arial, Sans-Serif; WHITE-SPACE: nowrap; BACKGROUND-COLOR: yellow; TEXT-DECORATION: none }
,我将其更改为
tr.Grid_RowItalicsBold td{ PADDING-RIGHT: 5px; PADDING-LEFT: 8px; FONT-WEIGHT: bold; FONT-SIZE: 8pt; OVERFLOW: hidden; COLOR: Black; LINE-HEIGHT: 15pt; FONT-STYLE: normal; FONT-FAMILY: Verdana, Arial, Sans-Serif; WHITE-SPACE: nowrap; BACKGROUND-COLOR: yellow; TEXT-DECORATION: none }
并且我使用javascript将该类分配给我的特定行。 :)
I have a problem setting the CSS class for each cell in a given table row. Initially I thought setting the parent row CSS would affect the style properties of cells,
but that doesn't work. Instead I have to loop through all the cells in a given row to updated the CSS class.
However this is not efficient. And it took a lot of time. Consider my situation: I have around 230 rows in which each row has 23 cells (totally 5290 cells).
Note: I don't use any frameworks. so please can you suggest an approach in native JS?
UPDATE :
its working fine using the Paolo's recommendation..
Initially my custom css class is been like this
.Grid_RowItalicsBold { PADDING-RIGHT: 5px; PADDING-LEFT: 8px; FONT-WEIGHT: bold; FONT-SIZE: 8pt; OVERFLOW: hidden; COLOR: Black; LINE-HEIGHT: 15pt; FONT-STYLE: normal; FONT-FAMILY: Verdana, Arial, Sans-Serif; WHITE-SPACE: nowrap; BACKGROUND-COLOR: yellow; TEXT-DECORATION: none }
And i changed this to
tr.Grid_RowItalicsBold td{ PADDING-RIGHT: 5px; PADDING-LEFT: 8px; FONT-WEIGHT: bold; FONT-SIZE: 8pt; OVERFLOW: hidden; COLOR: Black; LINE-HEIGHT: 15pt; FONT-STYLE: normal; FONT-FAMILY: Verdana, Arial, Sans-Serif; WHITE-SPACE: nowrap; BACKGROUND-COLOR: yellow; TEXT-DECORATION: none }
And i assigned this class to my specific rows using javascript. :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为什么你不能设置行的类并相应地调整你的CSS?
然后在 CSS 中:
无论哪种情况,假设表的 id 为“mytable”,您都可以为所有表行提供您想要的类,如下所示:
不过,如果您对整个表执行此操作,您也可以只给表标签本身一个类,然后执行以下操作:
Why can't you set the class of the row and adjust your css accordingly?
Then in CSS:
In either case, assuming the table has an id of "mytable" you could give all the table rows the class you want like so:
If you're doing this to the whole table, though, you might as well just give a class to the table tag itself then do:
看起来你仍然应该使用CSS选择器来做你想做的事情。也许是这样的:
It still seems like you should be using CSS selectors to do what you want. Maybe something like this:
我认为,如果您将某些单元格的属性设置为“继承”,它们将从表行继承这些属性。你尝试过吗?
I think that if you set some of your cell's properties to "inherit" they will inherit those properties from the table row. Have you tried that?