流程控制
1.1 条件语句 分支结构
单向分支
if (条件表达式) {
code...
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>if流程控制双向分支,if..else</title>
<style type="text/css">
div {
100px;
height: 50px;
border: 1px solid gray;
margin-top: 10px;
}
</style>
</head>
<body>
<h1>成绩查询</h1>
<hr>
<h2>输入你的成绩:</h2>
<input type="number" id="score">
<button onclick="makeScore()">查询</button>
<div id='res'></div>
<!--第一种:if..else双向分支-->
<script type="text/javascript">
function makeScore() {
//之前的写法3行:
// var scoreNum =document.getElementById('score')
// var Num = scoreNum.value;
// Num = Number(Num)
//精简一行写法如下:
var score=Number(document.getElementById('score').value)
if (score > 60) {
var resContent = `成绩为:${score},恭喜及格了`;
}else {
var resContent = `成绩为:${score},不及格继续努力哦`
}
document.getElementById('res').innerHTML = resContent;
}
</script>
</body>
</html>