Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何获取元素';javascript中的背景色是什么?_Javascript_Css - Fatal编程技术网

如何获取元素';javascript中的背景色是什么?

如何获取元素';javascript中的背景色是什么?,javascript,css,Javascript,Css,我需要获取以前由样式表定义的元素的背景色,以便确定将使用javascript动态创建的新元素的样式 我尝试使用'backgroundColor'属性,但返回值为空。首先需要使用js设置该值,然后可以使用属性(不是十六进制)检索该值。是否有其他替代方案,无需修改元素?比如使用偏移网络宽度 CSS: HTML: JSFiddle: 试试这个: function getStyle(element, property) { if (element.currentStyle) re

我需要获取以前由样式表定义的元素的背景色,以便确定将使用javascript动态创建的新元素的样式

我尝试使用'backgroundColor'属性,但返回值为空。首先需要使用js设置该值,然后可以使用属性(不是十六进制)检索该值。是否有其他替代方案,无需修改元素?比如使用偏移网络宽度

CSS:

HTML:

JSFiddle: 试试这个:

function getStyle(element, property) {
    if (element.currentStyle)
        return this.currentStyle[property];
    else if (getComputedStyle)
        return getComputedStyle(element, null)[property];
    else
        return element.style[property];
}
将此代码放在脚本文件的开头,并以这种方式访问它

function getColor(color){
    var box = document.getElementById("mybox");
    alert(getStyle(box, 'backgroundColor'));
}
编辑:“压缩”版本


这段代码让人想起prototype.js。。。为什么这是个坏主意?可能在IE9之前不支持defaultView@赫林顿·达克霍尔梅:今天的原型在哪里?好吧,这有点讽刺。:-)(注意:我曾经参与过这个原型项目,早在2005-2006年。)但是,如果它在重要的浏览器上不起作用(IE8拥有约23%的全球用户),这不是一个好主意。这一个可行。我的目标浏览器是那些只支持HTML5的浏览器。更好的是,我认为您可以安全地扩展DOM。对@T.J.Crowder?
function getColor(color){
    var box = document.getElementById("mybox");
    alert(box.style.backgroundColor);
}
function setColor(color){
    var box = document.getElementById("mybox");
    box.style.backgroundColor = color;
}
function getStyle(element, property) {
    if (element.currentStyle)
        return this.currentStyle[property];
    else if (getComputedStyle)
        return getComputedStyle(element, null)[property];
    else
        return element.style[property];
}
function getColor(color){
    var box = document.getElementById("mybox");
    alert(getStyle(box, 'backgroundColor'));
}
function getStyle(element, property) {
    return getComputedStyle ? getComputedStyle(element, null)[property] : 
    element.currentStyle ? element.currentStyle[property] : element.style[property];
}