1. 程式人生 > >使用HTML5 select標籤來實現更改網頁背景顏色

使用HTML5 select標籤來實現更改網頁背景顏色

<!doctype html>
<html>
<head>
<style>


</style>
<script>
function load(){
var test=document.getElementById("test");
document.body.style.backgroundColor=test.options[test.selectedIndex].value;
//獲得select的selectedIndex屬性來獲得option的value值,設定body的backgroundColor屬性
}
</script>
<meta charset="UTF-8">
</head>
<body>
<select id="test" onchange="load()"><!--在select selected的選項改變時觸發事件-->
<option value="white">白色</option>
<option value="yellow">黃色</option>
<option value="green">綠色</option>
<option value="red">紅色</option>
</select>
</body>
</html>