存储样式属性';在JavaScript中,将值转换为变量。

存储样式属性';在JavaScript中,将值转换为变量。,javascript,css,Javascript,Css,我想获取元素的背景属性。这是我的密码 <!doctype html> <html> <head> <style> div#pt { background: rgba(192,15,18,1.00); width: 200px; height: 200px; } </style> </head> <body> <div id="pt" onClick="pick()"><

我想获取元素的背景属性。这是我的密码

<!doctype html>
<html>
<head>
<style>
div#pt {
    background: rgba(192,15,18,1.00);
    width: 200px;
    height: 200px;
}
</style>
</head>

<body>
<div id="pt" onClick="pick()"></div>
<script>
function pick() {
debugger;
var color = document.getElementById("pt").style.background;
}
</script>
</body>
</html>

部门{
背景:rgba(192,15,18,1.00);
宽度:200px;
高度:200px;
}
函数pick(){
调试器;
var color=document.getElementById(“pt”).style.background;
}

我想这会管用,但不管用。它为
color
变量提供一个空字符串。我认为这不是获取背景属性值的正确方法。因此,请帮助我,如何获取后台属性的值。

使用
getComputedStyle
作为后备方法。您可以这样做:

var color =
   (document.getElementById("pt").style.background.length > 0) ?
     document.getElementById("pt").style.background.length : 
     getComputedStyle(document.getElementById("pt")).getPropertyValue("background");

换行符仅用于说明目的。

使用
getComputedStyle
作为后备。您可以这样做:

var color =
   (document.getElementById("pt").style.background.length > 0) ?
     document.getElementById("pt").style.background.length : 
     getComputedStyle(document.getElementById("pt")).getPropertyValue("background");

换行符仅用于说明目的。

使用
getComputedStyle
作为后备。您可以这样做:

var color =
   (document.getElementById("pt").style.background.length > 0) ?
     document.getElementById("pt").style.background.length : 
     getComputedStyle(document.getElementById("pt")).getPropertyValue("background");

换行符仅用于说明目的。

使用
getComputedStyle
作为后备。您可以这样做:

var color =
   (document.getElementById("pt").style.background.length > 0) ?
     document.getElementById("pt").style.background.length : 
     getComputedStyle(document.getElementById("pt")).getPropertyValue("background");

换行符仅用于说明目的。

style属性仅包含在
style
属性中指定或通过脚本设置的样式。在
style
元素或外部样式表中设置的样式在那里找不到,此时您需要针对不同浏览器使用不同的技术(除IE之外的所有浏览器都使用标准技术)

适用于IE

var color= document.getElementById("pt").currentStyle.background;
其他浏览器

var color= getComputedStyle(document.getElementById("pt")).getPropertyValue(background);

style属性仅包含在
style
属性中指定或通过脚本设置的样式。在
style
元素或外部样式表中设置的样式在那里找不到,此时您需要针对不同浏览器使用不同的技术(除IE之外的所有浏览器都使用标准技术)

适用于IE

var color= document.getElementById("pt").currentStyle.background;
其他浏览器

var color= getComputedStyle(document.getElementById("pt")).getPropertyValue(background);

style属性仅包含在
style
属性中指定或通过脚本设置的样式。在
style
元素或外部样式表中设置的样式在那里找不到,此时您需要针对不同浏览器使用不同的技术(除IE之外的所有浏览器都使用标准技术)

适用于IE

var color= document.getElementById("pt").currentStyle.background;
其他浏览器

var color= getComputedStyle(document.getElementById("pt")).getPropertyValue(background);

style属性仅包含在
style
属性中指定或通过脚本设置的样式。在
style
元素或外部样式表中设置的样式在那里找不到,此时您需要针对不同浏览器使用不同的技术(除IE之外的所有浏览器都使用标准技术)

适用于IE

var color= document.getElementById("pt").currentStyle.background;
其他浏览器

var color= getComputedStyle(document.getElementById("pt")).getPropertyValue(background);


element.style
对象只包含内联CSS。
window.getComputedStyle(元素)谢谢。我得到了它。这将使我的概念更加清晰。
element.style
对象只包含内联CSS。
window.getComputedStyle(元素)谢谢。我得到了它。这将使我的概念更加清晰。
element.style
对象只包含内联CSS。
window.getComputedStyle(元素)谢谢。我得到了它。这将使我的概念更加清晰。
element.style
对象只包含内联CSS。
window.getComputedStyle(元素)谢谢。我得到了它。这将使我的概念更加清晰。我应用了它,但它在Chrome中运行良好,但在浏览器的其他部分(firefox、internet explorer、Microsoft edge)中,它给出了一个空字符串。这对我来说是个大问题。我已经分别为IE(internet explorer)和其他浏览器提供了一个解决方案!您为internet explorer提出的建议不起作用。哦,是的,现在检查它检查编辑!仍然不工作它返回一个未定义的值。我应用了它,但它在Chrome中运行良好,但在浏览器的其余部分(firefox、internet explorer、Microsoft edge)中,它给出了一个空字符串。这对我来说是个大问题。我已经分别为IE(internet explorer)和其他浏览器提供了一个解决方案!您为internet explorer提出的建议不起作用。哦,是的,现在检查它检查编辑!仍然不工作它返回一个未定义的值。我应用了它,但它在Chrome中运行良好,但在浏览器的其余部分(firefox、internet explorer、Microsoft edge)中,它给出了一个空字符串。这对我来说是个大问题。我已经分别为IE(internet explorer)和其他浏览器提供了一个解决方案!您为internet explorer提出的建议不起作用。哦,是的,现在检查它检查编辑!仍然不工作它返回一个未定义的值。我应用了它,但它在Chrome中运行良好,但在浏览器的其余部分(firefox、internet explorer、Microsoft edge)中,它给出了一个空字符串。这对我来说是个大问题。我已经分别为IE(internet explorer)和其他浏览器提供了一个解决方案!您为internet explorer提出的建议不起作用。哦,是的,现在检查它检查编辑!仍然不工作,它返回一个未定义的值。后退意味着什么?我只使用了这一行getComputedStyle(document.getElementById(“pt”)).getPropertyValue(“背景”);它在Chrome上运行良好,但在浏览器的其他部分却不行。回退意味着,一种主方法不起作用的备份方法。回退意味着什么?我只使用了这一行getComputedStyle(document.getElementById(“pt”)).getPropertyValue(“背景”);它在Chrome上运行良好,但在浏览器的其他部分却不行。回退意味着,一种主方法不起作用的备份方法。回退意味着什么?我只使用了这一行getComputedStyle(document.getElementById(“pt”)).getPropertyValue(“背景”);它在Chrome上运行良好,但在浏览器的其他部分却不行。回退意味着,一种主方法不起作用的备份方法。回退意味着什么?我只使用了这一行getComputedStyle(document.getElementById(“pt”)).getPropertyValue(“背景”);它可以在Chrome中正常工作,但在浏览器的其他部分却不行。回退意味着,一种主方法不起作用的备份方法。