[英]Inline css value in javascript
How would I get a css property inside javascript! 我如何在JavaScript中获取CSS属性!
ex : 例如:
<style>
#body{
background:red;
}
</style>
<script>
function valid(form){
alert('enter');
var test = document.getElementById('body').style.background ;
alert(test');
}
</script>
</head>
<body id="body">
On alert I am not able to get the background color! 处于警报状态时,我无法获取背景色!
The style
property on elements only reflects the style information in the element itself (such as via the style
attribute in the markup), not anything applied by style rules. 元素上的
style
属性仅反映元素本身中的样式信息(例如通过标记中的style
属性),而不能反映样式规则所应用的任何内容。 To get those, you'd need getComputedStyle
: 要获得这些,您需要
getComputedStyle
:
var foo = document.getElementById('foo');
display("foo's background color is: " +
window.getComputedStyle(foo, null).getPropertyValue('background-color'));
Off-topic : Some of this stuff is made easier by libraries such as jQuery , Prototype , YUI , Closure , or any of several others . 离题 :诸如jQuery , Prototype , YUI , Closure或其他几种库使这些东西变得更容易。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.