javascript中用style来访问样式表,有什么区别
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style type="text/css">
div.special{
background-color: red;
height: 80px;
width: 80px;
margin: 5px;
}
</style>
<script type="text/javascript">
function getBackgroundColor(){
var oDiv1 = document.getElementById("div1");
alert(oDiv1.style.backgroundColor);
} //警告框返回的是空字符串
function changeBackgroundColor(){
var oDiv4 = document.getElementById("div4");
oDiv4.style.backgroundColor = "blue";
} //能正确改变颜色
</script>
</head>
<body>
<div id="div1"></div>
<div id="div2"></div>
<div id="div3"></div>
<input type="button" value="getBackgroundColor" onclick="getBackgroundColor()"/>
<div id="div4"></div>
<div id="div5"></div>
<div id="div6"></div>
<input type="button" value="changeBackgroundColor" onclick="changeBackgroundColor()"/>
</body>
</html>
慕瓜9220888
后知后觉88
相关分类