<!doctype html public "-//w3c//dtd html 4.01 transitional//en"
"http://www.w3c.org/tg/xhtml/dtd/xhtml-transitional.dtd">
<html>
<head>
<title>changecolor.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<link rel="stylesheet" type="text/css" href="http://www.php1.cn/"> <script type="text/javascript">
function changecolor(eventobj){
//获得css中的所有选择器
var cssset = document.stylesheets[0].rules;
//从css中获取所需要的选择器
var style1=cssset[0];
if(eventobj.value==黑色){
style1.style.backgroundcolor=black;
}else if(eventobj.value==红色){
style1.style.backgroundcolor=red;
}
}
</script>
</head>
<body>
<div class="style1"></div>
<input type="button" value="黑色" onclick="changecolor(this);">
<input type="button" value="红色" onclick="changecolor(this);">
</body>
</html>
css代码:
.style1{
width: 600px;
height: 400px;
background-color: black;
}