Html+js Code:
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en"
"Http://www.w3c.org/TG/xhtml/DTD/xhtml-transitional.dtd" >
<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) {
Get all selectors in CSS
var cssset = document.stylesheets[0].rules;
Get the desired selector from the CSS
var style1=cssset[0];
if (eventobj.value== "black") {
Style1.style.backgroundcolor= "BLACK";
}else if (eventobj.value== "red") {
Style1.style.backgroundcolor= "Red";
}
}
</script>
<body>
<div class= "Style1" ></div>
<input type= "button" value= "Black" onclick= "changecolor (this);" >
<input type= "button" value= "Red" onclick= "ChangeColor (this);" >
</body>
CSS code:
. style1{
width:600px;
height:400px;
Background-color:black;
}