Leo*_*Leo 6 html javascript css background-color getcomputedstyle
这是一个惊喜.以下代码似乎没有给我屏幕上的实际颜色:
h1 = document.querySelector("h1");
window.getComputedStyle(h1).color
Run Code Online (Sandbox Code Playgroud)
给rgb(0, 0, 0)我认为是正确的.然而
window.getComputedStyle(h1).backgroundColor
Run Code Online (Sandbox Code Playgroud)
给rgba(0, 0, 0, 0).我在屏幕上看到的实际背景颜色是白色.
我调用h1的元素在屏幕上可见.我期待得到实际的背景颜色.我得到的值(在rgba中)没有错,但也不是很有用.它只是告诉我背景是完全透明的 - 这不是一种颜色.
如何获得RGB的实际背景颜色?
如果你0在你的CSS中设置你的背景颜色:rgba(255,255,255,); getComputedStyle()将返回transparent(在某些浏览器中)而不是您的rgba值.
简单的解决方法是将alpha设置为高于0的值,例如rgba(255,255,255,0.01); 这将返回rgba(255,255,255,0.01)
| 归档时间: |
|
| 查看次数: |
2218 次 |
| 最近记录: |