JS 简单计算器
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>简单计算器</title>
</head>
<body>
<div style="width: 200px;margin: auto; background-color:rgb(184, 249, 237);">
<input type="text" size="5" id="num1" value="" />
<select id="ysf">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
<option value="%">%</option>
</select>
<input type="text" size="5" id="num2" value=""/>
<input type="button" value="计算" id="btn" />
</div>
<p id="res" style="width: 200px;margin: 10px auto; background-color:rgb(184, 249, 237);"></p>
<script>
//获取按钮注册事件
document.getElementById('btn').onclick=function(){
var num1 = document.getElementById('num1').value;
var num2 = document.getElementById('num2').value;
var ysf = document.getElementById('ysf').value;
// 简单判断输入的内容只能是数字
if (num1 == NaN || num2 == NaN){
window.alert('请输入数字进行计算');
}else{
// document.getElementById('res').innerHTML=num1+"---"+num2+"---"+ysf;
var res = document.getElementById('res');
if(ysf == '+'){
res.innerHTML = num1 + ysf + num2 + '=' + (parseInt(num1) + parseInt(num2));
};
if(ysf == '-'){
res.innerHTML = num1 + ysf + num2 + '=' + (num1 - num2);
};
if(ysf == '*'){
res.innerHTML = num1 + ysf + num2 + '=' + (num1 * num2);
};
if(ysf == '/'){
// 判断被除数不能为0
if(num2 == 0){
window.alert('被除数不能为0');
}else{
res.innerHTML = num1 + ysf + num2 + '=' + (num1 / num2);
}
};
}
};
</script>
</body>
</html>
九九乘法表
上图
js代码
<script>
document.write('<table border="1" width="800" align="center">')
for(var row = 9; row > 0; row--){
document.write('<tr>')
for(var col = 1; col <= row; col++){
document.write('<td>' + col + '*' + row + '=' + col * row + '</td>')
// console.log(col + '*' + row + '=' + col * row)
}
document.write('</tr>')
}
document.write('</table>')
</script>