请帮我。我想调用css文件中的css属性,但是得到空值。
<style>
#a {padding:10px;}
</style>
<div id="a"></div>
<div id="b" style="padding:10px;"></div>
<script>
alert(document.getElementById('a').style.padding);//''
alert(document.getElementById('b').style.padding);//'10px'
</script>
您不能以这种方式获取简写属性,但是可以使用以下方法:
alert(window.getComputedStyle(document.getElementById("a"), null).getPropertyValue("padding-top"));
编辑以添加来自BYossarians评论的链接:
https://developer.mozilla.org/zh-CN/docs/Web/API/HTMLElement.style
概要:
返回一个表示元素的style属性的对象。
https://developer.mozilla.org/en/docs/Web/API/window.getComputedStyle
概要:
在应用活动样式表并解析这些值可能包含的任何基本计算之后,getComputedStyle()会给出元素的所有CSS属性的值。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句