<!Doctype html>
<html>
<head>
<meta charset="utf-8">
<style>
.main {
width: 600px;
height: 600px;
border: 2px solid black;
margin: 0 auto;
background: #F0FFFF;
border-radius: 5px;
}
.screen {
margin: 10px auto;
width: 80%;
height: 10%;
border: 2px solid black;
background: #E6E6FA;
display: flex;
border-radius: 5px;
position: relative;
}
.operations{
line-height:60px;
font-size:20px;
}
.keyBoard {
margin: 10px auto;
width: 85%;
height: 80%;
border: 2px solid black;
background: LightCyan;
display: flex;
flex-wrap: wrap;
border-radius: 5px;
}
.keys {
width: 120px;
height: 20%;
margin: 10px 1.5px;
border: 2px solid black;
font-family: fantasy;
font-size: 20px;
text-align: center;
line-height: 100px;
font-weight: bold;
border-radius: 5px;
}
.keys:hover {
background: #FFE4B5;
}
#clear {
margin-left: 420px;
width: 58px;
height: 58px;
border: 2px solid black;
border-radius: 29px;
font-family: fantasy;
font-size: 20px;
text-align: center;
font-weight: bold;
position: fixed;
}
#clear:hover {
background: #FFE4B5;
}
</style>
</head>
<body>
<div class="main">
<div class="screen">
<span class="operations" id="shu"></span>
<span class="operations" id="operation"></span>
<span class="operations" id="beiShu"></span>
<div class="operations" id="clear">清空</div>
</div>
<div class="keyBoard">
<div class="keys" >7</div>
<div class="keys" >8</div>
<div class="keys" >9</div>
<div class="keys" >*</div>
<div class="keys" >4</div>
<div class="keys" >5</div>
<div class="keys" >6</div>
<div class="keys" >-</div>
<div class="keys" >1</div>
<div class="keys" >2</div>
<div class="keys" >3</div>
<div class="keys" >+</div>
<div class="keys" >0</div>
<div class="keys" >.</div>
<div class="keys" >=</div>
<div class="keys" >/</div>
</div>
</div>
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
<script>
//加函数
var add = () => {
let shuVal = $("#shu").html();
let beiShuVal = $("#beiShu").html();
let jianShu = Number(shuVal);
let beiJianShu = Number(beiShuVal);
if(shuVal == ""){
alert("请输入加数");
}
if(beiShuVal == ""){
alert("请输入被加数");
}
return jianShu+beiJianShu;
}
var sub = () => {
let shuVal = $("#shu").html();
let beiShuVal = $("#beiShu").html();
let jianShu = Number(shuVal);
let beiJianShu = Number(beiShuVal);
if(shuVal == ""){
alert("请输入被减数");
}
if(beiShuVal == ""){
alert("请输入减数");
}
return jianShu-beiJianShu;
}
var mul = () => {
let shuVal = $("#shu").html();
let beiShuVal = $("#beiShu").html();
let jianShu = Number(shuVal);
let beiJianShu = Number(beiShuVal);
if(shuVal == ""){
alert("请输入被乘数");
}
if(beiShuVal == ""){
alert("请输入乘数");
}
return jianShu*beiJianShu;
}
var chu = () => {
let shuVal = $("#shu").html();
let beiShuVal = $("#beiShu").html();
let jianShu = Number(shuVal);
let beiJianShu = Number(beiShuVal);
if(shuVal == ""){
alert("请输入被除数");
}
if(beiShuVal == ""){
alert("请输入除数");
}
return jianShu/beiJianShu;
}
//获取按键DOM
var keys = document.getElementsByClassName("keys");
//加减乘除操作开关
var witch = false;
//0加1减2乘3除
var operation = 0;
//按键监听函数
var listenKeys = () => {
for(let i=0; i < keys.length; i++){
keys[i].onclick = function(){
let type = this.innerText;
let beiShu = $("#shu");
let shu = $("#beiShu");
let operationNode = $("#operation");
console.log(type);
switch (type) {
case "1":
{
console.log("我到了");
if(witch){
shu.append(type);
}else{
beiShu.append(type);
}
break;
}
case "0":
{
if(witch){
shu.append(type);
}else{
beiShu.append(type);
}
break;
}
case "2":
{
if(witch){
shu.append(type);
}else{
beiShu.append(type);
}
break;
}
case "3":
{
if(witch){
shu.append(type);
}else{
beiShu.append(type);
}
break;
}
case "4":
{
if(witch){
shu.append(type);
}else{
beiShu.append(type);
}
break;
}
case "5":
{
if(witch){
shu.append(type);
}else{
beiShu.append(type);
}
break;
}
case "6":
{
if(witch){
shu.append(type);
}else{
beiShu.append(type);
}
break;
}
case "7":
{
if(witch){
shu.append(type);
}else{
beiShu.append(type);
}
break;
}
case "8":
{
if(witch){
shu.append(type);
}else{
beiShu.append(type);
}
break;
}
case "9":
{
if(witch){
shu.append(type);
}else{
beiShu.append(type);
}
break;
}
case "+":
{
witch = true;
operationNode.html(type);
operation = 0;
break;
}
case "-":
{
witch = true;
operationNode.html(type);
operation = 1;
break;
}
case "*":
{
witch = true;
operationNode.html(type);
operation = 2;
break;
}
case "/":
{
witch = true;
operationNode.html(type);
operation = 3;
break;
}
case "=":
{
switch(operation){
case 0:{
let res = add();
beiShu.html(res);
shu.html("");
operationNode.html("");
witch = false;
break;
}
case 1:{
let res = sub();
beiShu.html(res);
shu.html("");
operationNode.html("")
witch = false;
break;
}
case 2:{
let res = mul();
beiShu.html(res);
shu.html("");
operationNode.html("")
witch = false;
break;
}
case 3:{
let res = chu();
beiShu.html(res);
shu.html("");
operationNode.html("")
witch = false;
break;
}
}
break;
}
}
}
}
}
//清除函数
var clear = () => {
$("#clear").click( () => {
let beiShu = $("#shu");
let shu = $("#beiShu");
let operationNode = $("#operation");
beiShu.html("");
shu.html("");
operationNode.html("");
})
}
//主运行函数
var main = () => {
listenKeys();
clear();
}
main();
</script>
</body>
</html>
最后大概的效果是这样的,建议用chrome。