<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
#div1 {
width: 100px;
height: 100px;
background: pink;
display: block;
}
</style>
</head>
<body>
<input type="button" value="變紅" onclick="toColor('red')">
<input type="button" value="變綠" onclick="toColor('green')">
<input type="button" value="變藍" onclick="toColor('blue')">
<div id="div1"></div>
<script>
function toColor(n) {
var oDiv = document.getElementById('div1');
oDiv.style.background = n;
}
</script>
</body>
</html>
運行結果