switch语句相比较于if else语句性能更高
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script type="text/javascript">
window.onload = function(){
var iWeek = 3;
var oBody = document.getElementById('body01');
switch (iWeek){
case 1:
oBody.style.backgroundColor = 'red';
break;
case 2:
oBody.style.backgroundColor = 'gold';
break;
case 3:
oBody.style.backgroundColor = 'lawngreen';
break;
case 4:
oBody.style.backgroundColor = 'blue';
break;
case 5:
oBody.style.backgroundColor = 'blueviolet';
break;
default:
oBody.style.backgroundColor = 'hotpink';
}
}
</script>
</head>
<body id="body01">
</body>
</html>