简体   繁体   中英

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:

在此输入图像描述

  • TOP1 is red
  • TOP2 is green
  • TOP3 doesn't have a background color defined, yet it is green.

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.

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.

There is no way to calculate the background of 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. No CSS value can tell you this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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