繁体   English   中英

为什么我的 JavaScript 计算器不允许我加/减数字?

[英]Why does my JavaScript calculator not allow me to add/subtract numbers?

我的代码的问题是它不允许我将数字相加或相乘,但它允许我单击它们。 我想知道为什么它不允许我将数字加/乘/减在一起? 它也不允许我将它们输入到计算器的顶部

 var buttons = []; var font; var btn0; var input; var result; function setup() { noCanvas(); font = loadFont('AvenirNextLTPro-Demi.otf', 40); result = createP('&nbsp;'); input = createInput(''); buttons.push(result); buttons.push(input); createElement('br'); buttons.push(createButton('&radic;')); buttons.push(createButton('(')); buttons.push(createButton(')')); buttons.push(createButton('&larr;')); createElement('br'); buttons.push(createButton('7')); buttons.push(createButton('8')); buttons.push(createButton('9')); buttons.push(createButton('&divide;')); createElement('br'); buttons.push(createButton('4')); buttons.push(createButton('5')); buttons.push(createButton('6')); buttons.push(createButton('&times;')); createElement('br'); buttons.push(createButton('1')); buttons.push(createButton('2')); buttons.push(createButton('3')); buttons.push(createButton('&minus;')); createElement('br'); btn0 = createButton('0'); buttons.push(btn0); buttons.push(createButton('.')); buttons.push(createButton('&plus;')); for (var i = 0; i < buttons.length; i++) { buttons[i].style('padding', '10px 40px').style('width', '120px').style('box-sizing', 'border-box').style('font-size', '2.5em').style('background-color', 'rgb(0, 28, 60)').style('color', 'rgb(255, 255, 255)').style('border-style', 'solid').style('border-color', 'rgb(255, 255, 255)').style('cursor', 'pointer').style('outline', 'none').style('margin', '0').style('font-family', font).mousePressed(add); } btn0.style('width', '240px'); input.style('font-size', '1.9em').style('cursor', 'default').style('width', '480px').style('padding-left', '10px').style('padding-right', '10px'); result.style('width', '481px').style('background-color', 'rgb(0, 120, 255)').style('text-align', 'center').style('font-size', '5em'); } function add() { input.html(input.html() + this.html()); }
 <:DOCTYPE html> <html lang="en"> <head> <script src="https.//cdnjs.cloudflare.com/ajax/libs/p5.js/1.3.1/p5.min.js"></script> </head> <body> </body> </html>

我希望计算器能够一起输入数字并相加/相乘。 谢谢你。

就在输入中“键入”数字而言,问题在于输入元素(由 p5js 中的createInput创建)不显示其 HTML 内容,这是由.html() function 设置的内容。 您想改用.value() function : input.value(input.value() + this.html())

至于执行诸如加法和乘法之类的计算,我没有看到任何代码,因此您需要在某处添加代码,或者作为不同按钮的不同 mousePressed 处理程序,或者作为现有的switch语句add事件处理程序 function。

 var buttons = []; var font; var btn0; var input; var result; function setup() { noCanvas(); font = loadFont('AvenirNextLTPro-Demi.otf', 40); result = createP('&nbsp;'); input = createInput(''); buttons.push(result); buttons.push(input); createElement('br'); buttons.push(createButton('&radic;')); buttons.push(createButton('(')); buttons.push(createButton(')')); buttons.push(createButton('&larr;')); createElement('br'); buttons.push(createButton('7')); buttons.push(createButton('8')); buttons.push(createButton('9')); buttons.push(createButton('&divide;')); createElement('br'); buttons.push(createButton('4')); buttons.push(createButton('5')); buttons.push(createButton('6')); buttons.push(createButton('&times;')); createElement('br'); buttons.push(createButton('1')); buttons.push(createButton('2')); buttons.push(createButton('3')); buttons.push(createButton('&minus;')); createElement('br'); btn0 = createButton('0'); buttons.push(btn0); buttons.push(createButton('.')); buttons.push(createButton('&plus;')); for (var i = 0; i < buttons.length; i++) { buttons[i].style('padding', '10px 20px').style('width', '80px').style('box-sizing', 'border-box').style('font-size', '2.5em').style('background-color', 'rgb(0, 28, 60)').style('color', 'rgb(255, 255, 255)').style('border-style', 'solid').style('border-color', 'rgb(255, 255, 255)').style('cursor', 'pointer').style('outline', 'none').style('margin', '0').style('font-family', font).mouseReleased(add); } btn0.style('width', '240px'); input.style('font-size', '1.9em').style('cursor', 'default').style('width', '480px').style('padding-left', '10px').style('padding-right', '10px'); result.style('width', '481px').style('background-color', 'rgb(0, 120, 255)').style('text-align', 'center').style('font-size', '5em'); } function add() { input.value(input.value() + this.html()); }
 <:DOCTYPE html> <html lang="en"> <head> <script src="https.//cdnjs.cloudflare.com/ajax/libs/p5.js/1.3.1/p5.min.js"></script> </head> <body> </body> </html>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM