js获取css样式的问题(新手)
<!DOCTYPE html><html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
.box1{
width: 100px;
height: 100px;
background-color: orangered;
}
</style>
</head>
<body>
<div class="box1"></div>
<button type="button" id="color" onclick="gaiColor">点我更改颜色</button>
<button type="button" id="color" onclick="getColor">点我获取颜色</button>
</body>
<script type="text/javascript">
var a = document.getElementsByClassName("box1");
var getColor = function(){
alert(a.currentstyle.backgroundColor);
}
var gaiColor = function(){
a.style.backgroundColor = "red";
}
</script>
</html>
这两个获取样式和更改样式的按钮无效 a.currentstyle未定义 getColor执行会报错
button.onclick传字符串是eval执行执行函数名需要加括号
页:
[1]