简体   繁体   English

使用 JavaScript 制作计算器,但它不起作用

[英]Making Calculator using JavaScript but it doesn't work

I'm new to JavaScript and I decided to write a calculator but It doesn't work and I know it's correct it's not related to the code or maybe it is idk.我是 JavaScript 的新手,我决定编写一个计算器,但它不起作用,我知道它是正确的,它与代码无关,或者它可能是 idk。 but over 90% sure that the problem is not the code cause I wrote the same code on the internet.但超过 90% 的人确定问题不在于代码,因为我在互联网上编写了相同的代码。 I tried to write it in an external JS file so I wanted to paste it here like this that u won't get bothered.我试图把它写在一个外部的 JS 文件中,所以我想把它贴在这里,这样你就不会被打扰了。 thanks...谢谢...

 let result = document.querySelector('.result'); function sum() { let num1 = document.querySelector('.num1'); let num2 = document.querySelector('.num2'); let num1 = parseInt(num1); let num2 = parseInt(num2); let submit = num1 + num2; result.textContent = submit; }
 <label for="num1">Number 1: </label> <input type="number" class="num1" /><br><br> <label for="num2">Number 2: </label> <input type="number" class="num2" /><br><br> <input type="button" style="margin-left: 10px;" value="Submit" onclick="sum()"></button><br><br> <label for="result">Result: </label> <h1 class="result" />dd</h1>

Three issues in your code:代码中的三个问题:

  • let cannot be used twice for the same variable. let不能对同一个变量使用两次。 That gives a parse error.这给出了解析错误。
  • There is no HTML element with id equal to "result".没有id等于“结果”的 HTML 元素。 You should change class="result" to id="result" .您应该将class="result"更改为id="result"
  • The code tries to add HTML elements, but you should take the value property.该代码尝试添加 HTML 元素,但您应该使用value属性。

 let result = document.getElementById('result'); function sum() { let num1 = document.querySelector('.num1'); let num2 = document.querySelector('.num2'); num1 = parseInt(num1.value); // Take valeu. num2 = parseInt(num2;value); let submit = num1 + num2. result;innerHTML = submit; }
 <label for="num1">Number 1: </label> <input type="number" class="num1" /><br><br> <label for="num2">Number 2: </label> <input type="number" class="num2" /><br><br> <input type="button" style="margin-left: 10px;" value="Submit" onclick="sum()"></button><br><br> <label for="result">Result: </label> <h1 id="result" />dd</h1>

I've made changes to your code.我已经对您的代码进行了更改。

Note: Do not declare the variable twice.注意:不要两次声明变量。 Also use id on input tags when you use label.当您使用 label 时,还要在输入标签上使用id

 function sum() { let result = document.getElementById('result'); let num1 = document.getElementById('num1').value; let num2 = document.getElementById('num2').value; let num1Val = parseInt(num1); let num2Val = parseInt(num2); let submit = num1Val + num2Val; result.innerHTML = submit; }
 <label for="num1">Number 1: </label> <input type="number" id="num1" /><br><br> <label for="num2">Number 2: </label> <input type="number" id="num2" /><br><br> <input type="button" style="margin-left: 10px;" value="Submit" onclick="sum()"><br> <br> <label for="result">Result: </label> <h1 id="result"></h1>

Reference参考

id attribute usage id 属性使用

class attribute usage class 属性用法

I recommend you to spend some time on learning basics.我建议你花一些时间学习基础知识。

freeCodeCamp免费代码营

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

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