HTML5+js实现一个简单的计算器
全部代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>简单的计算器</title>
</head>
<body onload="onload()">
<div style="width: 12.25rem;height: 9.5rem;background-color: #00B3EE;">
<div id="shuru">
<div style="margin-top: 0.3125rem;width: 9.375rem;height: 2.5rem;text-align: right;padding-right: 0.625rem;font-size: 1.25rem;">
<input type="text" id="screenName" onfocus="jsq(this)" style="text-align: right;"/>
</div>
</div>
<div>
<input type="button" id="7" onclick="jsq(this.id)" value="7" style="width: 1.875rem;" />
<input type="button" id="8" onclick="jsq(this.id)" value="8" style="width: 1.875rem;" />
<input type="button" id="9" onclick="jsq(this.id)" value="9" style="width: 1.875rem;" />
<input type="button" id="back" onclick="huitui()" value="回退" style="font-size: 0.625rem;" />
<input type="button" id="c" onclick="qingchu()" value="清除" style="font-size: 0.625rem;" />
<br />
<input type="button" id="4" onclick="jsq(this.id)" value="4" style="width: 1.875rem;" />
<input type="button" id="5" onclick="jsq(this.id)" value="5" style="width: 1.875rem;" />
<input type="button" id="6" onclick="jsq(this.id)" value="6" style="width: 1.875rem;" />
<input type="button" id="*" onclick="jsq(this.id)" value="*" style="width: 1.875rem;" />
<input type="button" id="/" onclick="jsq(this.id)" value="/" style="width: 1.875rem;" />
<br />
<input type="button" id="1" onclick="jsq(this.id)" value="1" style="width: 1.875rem;" />
<input type="button" id="2" onclick="jsq(this.id)" value="2" style="width: 1.875rem;" />
<input type="button" id="3" onclick="jsq(this.id)" value="3" style="width: 1.875rem;" />
<input type="button" id="+" onclick="jsq(this.id)" value="+" style="width: 1.875rem;" />
<input type="button" id="-" onclick="jsq(this.id)" value="-" style="width: 1.875rem;" />
<br />
<input type="button" id="0" onclick="jsq(this.id)" value="0" style="width: 1.875rem;" />
<input type="button" id="00" onclick="jsq(this.id)" value="00" />
<input type="button" id="." onclick="jsq(this.id)" value="." style="width: 1.875rem;" />
<input type="button" id="%" onclick="jsq(this.id)" value="%" style="width: 1.875rem;" />
<input type="button" id="deng" onclick="deng()" value="=" style="width: 1.875rem;" />
</div>
</div>
<script>
var num = 0;
function jsq(num){
document.getElementById('screenName').value += document.getElementById(num).value;
}
function deng(){
document.getElementById('screenName').value = eval(document.getElementById("screenName").value);
}
function qingchu(){
document.getElementById("screenName").value = null;
document.getElementById("screenName").focus();
}
function huitui(){
var arr = document.getElementById("screenName");
arr.value = arr.value.substring(0,arr.value.length - 1);
}
function onload(){
document.getElementById("screenName").focus();
}
</script>
</body>
</html>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端