简单计算器
程序员文章站
2022-03-13 13:54:58
...
计算器
图片展示
代码如下
<!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>
<input type="text" id="x" value="">
<select name="" id="cs">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<input type="text" id="y" value="">
<input type="button" value="=" id="btn">
<p id="he">
<script>
// 获取按钮注册事件
document.getElementById('btn').onclick=function(){
var x=parseInt(document.getElementById('x').value);
var y=parseInt(document.getElementById('y').value);
var cs=document.getElementById('cs').value;
// var cs=document.getElementById('cs').innerText;
// document.getElementById('he').innerText=x+"---"+cs+"---"+y;
var he=parseInt(document.getElementById('he').value);
switch(cs){
case '+':
he = x + y;
break;
case '-':
he = x - y;
break;
case '*':
he = x * y;
break;
case '/':
he = x / y;
break;
default:
'';
}
return document.getElementById('he').innerText=he;
}
</script>
</body>
</html>