JavaScript实现简单计算器
程序员文章站
2022-03-01 20:41:21
...
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>calc</title>
</head>
<body>
<input type="number" name="" id="a_number" value="1">
<select name="yunsuanfu" id="yunsuanfu" >
<option value="">请选择运算符</option>
<option value="+" >+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<input type="number" name="" id="b_number">
的结果是:
<span id="jieguo">【 】</span>
<br>
<button id="jisuan" onclick="jisuan()">计算</button>
</body>
<script>
function jisuan() {
var a= document.getElementById("a_number").value;
var b= document.getElementById("b_number").value;
var yunsuanfu= document.getElementById("yunsuanfu").value;
var jieguo ;
a = parseFloat(a);
b = parseFloat(b);
if(isNaN(a) || isNaN(b)){
jieguo="请输入数字";
}
if(yunsuanfu===''){
jieguo="还没有选择运算符"
}else if(yunsuanfu==='+'){
jieguo = a+b;
}else if(yunsuanfu==='-'){
jieguo = a-b;
}else if(yunsuanfu==='*'){
jieguo= a*b;
}else{
if(b===0){
jieguo="除数不能为0"
}
jieguo= a/b;
}
document.getElementById("jieguo").innerText='【'+ jieguo +'】';
}
</script>
</html>
![简单计算器](https://img.php.cn/upload/image/416/193/351/1637247706719366.png "简单计算器")
下一篇: 怎样从编程开发中获得乐趣