From: http://www.iteye.com/topic/166395
备忘一下:)
备忘一下:)
<script type="text/javascript">
var mydiv=document.getElementById("test")
function getCurrentStyle(obj, cssproperty, csspropertyNS){
if(obj.style[cssproperty]){
return obj.style[cssproperty];
}
if (obj.currentStyle) {// IE5+
return obj.currentStyle[cssproperty];
}else if (document.defaultView.getComputedStyle(obj, null)) {// FF/Mozilla
var currentStyle = document.defaultView.getComputedStyle(obj, null);
var value = currentStyle.getPropertyValue(csspropertyNS);
if(!value){//try this method
value = currentStyle[cssproperty];
}
return value;
}else if (window.getComputedStyle) {// NS6+
var currentStyle = window.getComputedStyle(obj, "");
return currentStyle.getPropertyValue(csspropertyNS);
}
}
alert(getCurrentStyle(mydiv, "backgroundColor", "background-color")); //alerts "yellow"
</script>
本文介绍了一种跨浏览器的方法来获取元素的CSS属性值,包括在IE和Firefox等浏览器中如何使用JavaScript来实现。通过一个简单的例子展示了如何获取div元素的背景颜色。
4万+

被折叠的 条评论
为什么被折叠?



