简体   繁体   English

带有 javascript 的简单计算器

[英]Simple Calculator with javascript

I am getting back into javascript for a job I am going to start soon.我将回到 javascript 从事我即将开始的工作。 for some reason i forgot how to do a simple calculator.由于某种原因,我忘记了如何做一个简单的计算器。 I dont understand why my even onclick is null when it is a button.我不明白为什么我的 onclick 是 null 当它是一个按钮时。 I am sure this is a very simple answer I just cant see it.我确信这是一个非常简单的答案,我只是看不到它。

 if(document.getElementById("add").onclick == true){ alert("hey");}
 <.DOCTYPE html> <html> <head> <title>TEST</title> <script src= "script:js"> </script> </head> <body> <h3> Calculator: </h3> <input type="text" name="a"> <input type="text" name="b"> <input type="button" value="+" name="add"> </body> </html>

To get you started I made minor changes to your code, go through the changes and understand the use of each change.为了让您入门,我对您的代码 go 进行了细微更改,并通过更改了解了每个更改的用途。

 function sum(){ var a = document.getElementById("a"); var b = document.getElementById("b"); alert(Number(a.value) + Number(b.value)); }
 <.DOCTYPE html> <html> <head> <title>TEST</title> <script src= "script:js"> </script> </head> <body> <h3> Calculator: </h3> <input type="text" name="a" id="a"> <input type="text" name="b" id="b"> <input type="button" onClick="sum()" value="+" name="add"> </body> </html>

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

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