JavaScript语句
if 语句 - 只有当指定条件为 true 时,使用该语句来执行代码
<script type="text/javascript">
if(5>3){
console.log(`5大于3`)
}
</script>
if…else 语句 - 当条件为 true 时执行代码,当条件为 false 时执行其他代码
<script type="text/javascript">
var sex= `男`
if (sex == '男') {
console.log('去男澡堂')
} else {
console.log('去女澡堂')
}
</script>
if…else if…else 语句- 使用该语句来选择多个代码块之一来执行
<script type="text/javascript">
var chengji = 90
if (chengji>=90){
console.log(`A-优秀`)
}else if(chengji>=80){
console.log(`B-良好`)
}else if(chengji>=60){
console.log(`C-合格`)
}else if(chengji>=59){
console.log(`D-不及格`)
}
</script>
if简易计算器
<style type="text/css">
.input{
width: 100px;
height: 30px;
background: #BFBFBF;
outline: none;
border: 0;
font-size: 15px;
text-align: center;
}
</style>
</head>
<body>
<input type="number" id="box1" class="input">
<select name="" id="box2" class="input">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<input type="number" id="box3" class="input">
<input type="button" id="box4" class="input" value="=">
<input type="number" id="box6" class="input">
</body>
<script type="text/javascript">
var box1 = document.getElementById(`box1`)
var box3 = document.getElementById(`box3`)
var box6 = document.getElementById(`box6`)
var box2 = document.getElementById(`box2`)
var box4 = document.getElementById(`box4`)
box4.onclick = function() {
var box1Value = box1.value * 1
var box3Value = box3.value * 1
console.log(box1Value);
var box2Value = box2.value
if (box2Value == '+') {
box6.value = box1Value + box3Value
} else if (box2Value == '-') {
box6.value = box1Value - box3Value
console.log(box6.value)
} else if (box2Value == '*') {
box6.value = box1Value * box3Value
console.log(box6.value)
} else if (box2Value == '/') {
box6.value = box1Value / box3Value
console.log(box6.value)
}
}
</script>
switch
简单计算器
<style type="text/css">
.input {
width: 100px;
height: 30px;
background: #BFBFBF;
outline: none;
border: 0;
font-size: 15px;
text-align: center;
}
</style>
</head>
<body>
<input type="number" id="box1" class="input">
<select name="" id="box2" class="input">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<input type="number" id="box3" class="input">
<input type="button" id="box4" class="input" value="=">
<input type="number" id="box6" class="input">
</body>
<script type="text/javascript">
var box1 = document.getElementById(`box1`)
var box3 = document.getElementById(`box3`)
var box6 = document.getElementById(`box6`)
var box2 = document.getElementById(`box2`)
var box4 = document.getElementById(`box4`)
box4.onclick = function() {
var box1Value = box1.value * 1
var box3Value = box3.value * 1
console.log(box1Value);
var box2Value = box2.value
switch (box2Value) {
case `+`:
box6.value = box1Value + box3Value
break
case `-`:
box6.value = box1Value - box3Value
break
case `*`:
box6.value = box1Value * box3Value
break
case `/`:
box6.value = box1Value / box3Value
case `+`:
box6.value = box1Value + box3Value
break
}
}
</script>
</html>
</head>
<body>
<input type="number" id="box1" class="input">
<select name="" id="box2" class="input">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<input type="number" id="box3" class="input">
<input type="button" id="box4" class="input" value="=">
<input type="number" id="box6" class="input">
</body>
<script type="text/javascript">
var box1 = document.getElementById(`box1`)
var box3 = document.getElementById(`box3`)
var box6 = document.getElementById(`box6`)
var box2 = document.getElementById(`box2`)
var box4 = document.getElementById(`box4`)
box4.onclick = function() {
var box1Value = box1.value * 1
var box3Value = box3.value * 1
console.log(box1Value);
var box2Value = box2.value
switch (box2Value) {
case `+`:
box6.value = box1Value + box3Value
break
case `-`:
box6.value = box1Value - box3Value
break
case `*`:
box6.value = box1Value * box3Value
break
case `/`:
box6.value = box1Value / box3Value
case `+`:
box6.value = box1Value + box3Value
break
}
}
</script>
for循环
<script type="text/javascript">
for(var i = 0;i<10;i++){
console.log(i)
}
</script>
99乘法表
<script type="text/javascript">
var stt = " "
for (var i = 1; i <= 9; i++) {
for (var k = 1; k <= i; k++) {
var a = i + '*' + k + '=' + i * k + ' '
stt += a
}
stt += '<br />'
}
console.log(stt)
document.write(stt)
</script>
while
<script type="text/javascript">
var i = 0
while(i<100){
console.log('hello world')
i++
}
var a = 100
while(a>0){
console.log('hi')
a--
}
</script>
<script type="text/javascript">
var a = 0
do {
console.log('1000')
a++
} while (a < 100)
</script>
将可能会报错的代码放在try中
catch块中就是我们对错误的处理方式
finally块中的代码不管是否产生错误都会执行
<script type="text/javascript">
try {
console.log(a)
} catch (err) {
console.log(err)
}finally{
console.log(1000)
}
</script>