实例:
<html>
<head>
<link href = "event2.css" rel = "stylesheet" type = "text/css"/>
<script>
function change(eobj){
//获取event2.css中所有选择器
var ocssRules = document.styleSheets[0].rules;//0表示得到引入的第一个css文件
//从ocssRules集合中取出希望的class选择器
var style1 = ocssRules[0];//0表示取出第一个class选择器
if(eobj.value == "red"){
style1.style.backgroundColor = "red";
}else{
style1.style.backgroundColor = "blue";
}
}
</script>
</head>
<body>
<div id = "mystyle" class = "div1">
div1
</div>
<input type = "button" value="red" onclick = "change(this)"/>
<input type = "button" value="blue" onclick = "change(this)"/>
</body>
</html>