获取元素的真实背景色? [英] Getting the real background-color of an element?

查看:71
本文介绍了获取元素的真实背景色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我想获取指定对象的 real 背景颜色,在这里, real 表示人们看到的内容,例如,给出以下代码:

Currently I want to get the real background-color of a specified object, here, the real means what the people see, for instance, given the following code:

<div id="foo" style="background-color: red">
    I am red
    <span id="bar">
         I have no background, but I am red
    </span>
</div>

span#bar的实际背景色应为rbg(255,0,0).

the real background-color of span#bar should be rbg(255,0,0).

到目前为止,我的操作类似于.但是我认为我的解决方案有些愚蠢,或者可能存在缺陷.所以我想知道是否还有更好的方法?

So far I am doing it like this. But I think my solution is somewhat stupid, or maybe there's defect in it. So I wonder if there's a better way to do that?

先谢谢您了:)

推荐答案

尝试

window.getComputedStyle(element, null).getPropertyValue("background-color")

这种方法既简单又本机.但是IE8-不支持.参见 https://developer.mozilla.org/en/docs/DOM/window .getComputedStyle

This approach is simple and native. But IE8- don't support. See https://developer.mozilla.org/en/docs/DOM/window.getComputedStyle

这篇关于获取元素的真实背景色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆