效果展示
代码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style type="text/css">
.redBackground{
background-color: red;
}
</style>
</head>
<body>
<label id="showText">显示的文字</label>
<button type="button" onclick="changeRed();">红色背景</button>
<script type="text/javascript">
function changeRed(){
// className即是设置类名
document.getElementById("showText").className="redBackground";
}
</script>
</body>
</html>