use*_*240 3 html javascript css
请帮我.我想调用位于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>
Run Code Online (Sandbox Code Playgroud)
你无法以这种方式获得速记属性,但你可以使用这个:
alert(window.getComputedStyle(document.getElementById("a"), null).getPropertyValue("padding-top"));
Run Code Online (Sandbox Code Playgroud)
编辑以添加来自BYossarians评论的链接:
https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement.style
摘要:
返回表示元素样式属性的对象.
https://developer.mozilla.org/en/docs/Web/API/window.getComputedStyle
摘要:
getComputedStyle()在应用活动样式表并解析这些值可能包含的任何基本计算后,给出元素的所有CSS属性的值.