简体   繁体   English

如何获得没有背景的元素颜色?

[英]How to get element color which has no background?

How to efficiently get color of element which has no background, but one of his parent has? 如何有效地获得没有背景的元素的颜色,但是他的父母之一呢? For example this is 3 levels depth set: 例如,这是3级深度设置:

在此输入图像描述

  • TOP1 is red TOP1是红色的
  • TOP2 is green TOP2是绿色的
  • TOP3 doesn't have a background color defined, yet it is green. TOP3没有定义背景颜色,但它是绿色的。

I have no problem writing a function to iretate through parents and find backgroundColor property but I believe it's not really reliable and cross browserly would suck. 我没有问题写一个函数来通过父母发现并查找backgroundColor属性,但我相信它不是真的可靠而且浏览器会很糟​​糕。

UPDATE UPDATE

I may want to clarify why I want such a thing. 我可能想澄清为什么我想要这样的事情。

One of my cases: I am building sticky header for a table which will clone the first set of columns of a table and stick them(position: fixed) when user scrolls very wide table. 我的一个案例:我正在构建一个表的粘性标题,它将克隆表的第一组列并在用户滚动非常宽的表时粘贴它们(位置:固定)。 So they can see the columns they are viewing. 因此他们可以看到他们正在查看的列。

Sometimes table has styles associated with it and columns have colors which may be inherited from table, it's container or actually anywhere. 有时表有与之关联的样式,列的颜色可以从表,容器或实际上的任何地方继承。 That's why I insist on using JavaScript solution on this. 这就是我坚持在此使用JavaScript解决方案的原因。

There is no way to calculate the background of TOP3. 没有办法计算TOP3的背景。 If its background is not set, it is transparent. 如果未设置其背景,则它是透明的。 As such, it has the color of whatever is below it. 因此,它具有低于它的任何颜色。

Example: 例:

 <div style="background: red; width: 100px; height: 100px"> TOP1 <div style="background: green; width: 80px; height: 80px"> TOP2 <div style="width: 60px; height: 100px; border: solid 1px yellow"> TOP3 </div> </div> </div> 

Preview: 预习:

Here, TOP3 is half green, half white. TOP3是半绿色,半白色。 No CSS value can tell you this. 没有CSS值可以告诉你这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM