Javascript 如何使用按钮进行加法、乘法和减法运算

Javascript 如何使用按钮进行加法、乘法和减法运算,javascript,html,css,events,Javascript,Html,Css,Events,当我点击程序中的按钮时,它们会记录在控制台中,但是如果我想添加这些数字,我该如何将它们组合在一起呢?因此,如果我按1+5,然后我按相等按钮,它将弹出: 1. + 五,= 那么我怎样才能得到6,或者任何其他的计算 我还想知道,当按下数字时,我是如何将数字显示在黑匣子(输入/屏幕)中的 //更改操作颜色的颜色 //多色 const colorMultiply=document.getElementById('multiply') colorMultiply.style.backgroundColo

当我点击程序中的按钮时,它们会记录在控制台中,但是如果我想添加这些数字,我该如何将它们组合在一起呢?因此,如果我按1+5,然后我按相等按钮,它将弹出: 1. + 五,=

那么我怎样才能得到6,或者任何其他的计算

我还想知道,当按下数字时,我是如何将数字显示在黑匣子(输入/屏幕)中的

//更改操作颜色的颜色
//多色
const colorMultiply=document.getElementById('multiply')
colorMultiply.style.backgroundColor=“绿色”
//分色
const colorddivide=document.getElementById('divide')
colorddivide.style.backgroundColor=“红色”
//减色
const colorSubtract=document.getElementById('subtract')
colorSubtract.style.backgroundColor=“蓝色”
//添加颜色
const colorAdd=document.getElementById('add'))
colorAdd.style.backgroundColor=“黄色”
//将数字的字体改为蓝色(我是这样做的,以防任何人想要一个数字的颜色)
//让它更容易
常量number1=document.getElementById('number1')
number1.style.color=“蓝色”
常量number2=document.getElementById('number2')
number2.style.color=“蓝色”
常量number3=document.getElementById('number3')
number3.style.color=“蓝色”
常量number4=document.getElementById('number4')
编号4.style.color=“蓝色”
常量number5=document.getElementById('number5')
number5.style.color=“蓝色”
常量number6=document.getElementById('number6')
编号6.style.color=“蓝色”
常量number7=document.getElementById('number7')
编号7.style.color=“蓝色”
常量number8=document.getElementById('number8')
编号8.style.color=“蓝色”
常量number9=document.getElementById('number9')
编号9.style.color=“蓝色”
常量number0=document.getElementById('number0')
number0.style.color=“蓝色”
常量decimal=document.getElementById('decimal')
decimal.style.color=“蓝色”
//更改“清除”按钮的颜色
const clear=document.getElementById('clear'))
clear.style.color=“白色”
clear.style.backgroundColor=“黑色”
///////////////////////////////////////////////////////////////////////////////////////////////////////
//然后,我们要在“清除”按钮之前插入“memoryStoreButton”:
var memoryStoreButton=document.createElement(“按钮”);
memoryStoreButton.innerHTML=“MS”;
清除.before(MemoryStore按钮)//在清除之前按下按钮
//然后我们要在memoryStoreButton之前加上memoryClearButton`
var memoryClearButton=document.createElement(“按钮”);
memoryClearButton.innerHTML=“MC”;
memoryStoreButton.before(memoryClearButton)//在清除之前按下按钮
//最后,在memoryClearButton之前加上memoryRestoreButton`
var memoryRestoreButton=document.createElement(“按钮”);
memoryRestoreButton.innerHTML=“MR”;
memoryClearButton.before(MemoryRestorButton)//在清除之前按下按钮
///////////////////////////////////////////////////////////////////////////////////////////////////////////
//按下了什么数字
document.querySelector(“.buttons8”).onclick=function(e){
if(e.target.nodeName==“按钮”){
log(`${e.target.dataset.num}`);
//做点什么
}
}
*{
保证金:0;
填充:0;
框大小:边框框;
}
身体{
最小高度:100vh;
显示器:flex;
对齐项目:居中;
证明内容:中心;
}
.计算器{
弹性:0.40%;
}
.屏幕8{
宽度:100%;
字体大小:7rem;
填充:0.5雷姆;
背景:rgb(41,41,56);
颜色:白色;
边界:无;
}
.按钮8{
显示器:flex;
柔性包装:包装;
过渡:所有0.5s线性;
}
钮扣{
弹性:0.25%;
边框:1px纯黑;
填充:0.250;
过渡:一切都很轻松;
}
.btn灰色{
背景:rgb(224);
}
.btn8{
字号:4rem;
}

计算器8
*
/
-
+
.
9
8.
7.
6.
5.
4.
3.
2.
1.
0
=
C
您可以使用
eval()
方法计算表达式,但请记住,使用
eval()
存在安全风险-切勿尝试计算可能来自应用程序外部的代码或值

在下面的代码中,我使用了一个数组来跟踪所有单击的参数。当用户单击
=
时,数组被合并成一个字符串并进行计算。您可能还需要添加一些检查以确保表达式有效,比如在计算之前使用for循环进行验证

在下面测试并运行:

//更改操作颜色的颜色
//多色
const colorMultiply=document.getElementById('multiply')
colorMultiply.style.backgroundColor=“绿色”
//分色
const colorddivide=document.getElementById('divide')
colorddivide.style.backgroundColor=“红色”
//减色
const colorSubtract=document.getElementById('subtract')
colorSubtract.style.backgroundColor=“蓝色”
//添加颜色
const colorAdd=document.getElementById('add'))
colorAdd.style.backgroundColor=“黄色”
//将数字的字体改为蓝色(我是这样做的,以防任何人想要一个数字的颜色)
//让它更容易
常量number1=document.getElementById('number1')
number1.style.color=“蓝色”
常量number2=document.getElementById('number2')
number2.style.color=“蓝色”
常量number3=document.getElementById('number3')
number3.style.color=“蓝色”
常量number4=document.getElementById('number4')
编号4.style.color=“蓝色”
常量number5=document.getElementById('number5')
number5.style.color=“蓝色”
常量number6=document.getElementById('number6')
编号6.style.color=“蓝色”
常量number7=document.getElementById('number7')
编号7.style.color=“蓝色”
常量number8=document.getElementById('number8')
编号8.style.color=“蓝色”
常量number9=document.getElementById('number9')
编号9.style.color=“蓝色”
常量number0=document.getElementById('number0')
number0.style.color=“蓝色”
常量decimal=document.getElementById('decimal')
decimal.style.color=“蓝色”
//改变