2 回答

TA貢獻1842條經(jīng)驗 獲得超22個贊
const suma = document.getElementById('suma');
const resta = document.getElementById('resta');
const multiplicacion = document.getElementById('multiplicacion');
const division = document.getElementById('division');
const output = document.getElementById('output');
const num1 = document.getElementById('num1');
const num2 = document.getElementById('num2');
suma.addEventListener('click', ()=>{
output.innerHTML = `${parseFloat(num1.value) + parseFloat(num2.value)}`;
});
resta.addEventListener('click', ()=>{
output.innerHTML = `${parseFloat(num1.value) - parseFloat(num2.value)}`;
});
multiplicacion.addEventListener('click', ()=>{
output.innerHTML = `${parseFloat(num1.value) * parseFloat(num2.value)}`;
});
division.addEventListener('click', ()=>{
output.innerHTML = `${parseFloat(num1.value) / parseFloat(num2.value)}`;
});
<!DOCTYPE html>
<html dir="ltr">
<head>
<meta charset="utf-8">
<title> Calculadora </title>
</head>
<body>
<p>
<input type = "number" id = "num1" />
<input type = "number" id = "num2" />
</p>
<p> <strong> ?Qué operación desea hacer? </strong> </p>
<p>
<input type = "button" value = "Sumar" id = "suma" />
<input type = "button" value = "Restar" id = "resta" />
<input type = "button" value = "Multiplicar" id = "multiplicacion" />
<input type = "button" value = "Dividir" id = "division" />
</p>
<p id='output' style='color: red;'></p>
<script src="script.js"> </script>
</body>
</html>

TA貢獻1818條經(jīng)驗 獲得超3個贊
您的代碼中存在一些問題。
您沒有使用
click
事件來調(diào)用您的函數(shù)你沒有
op
在你的函數(shù)中定義您在函數(shù)中添加了控制臺日志,這意味著您在函數(shù)中調(diào)用該函數(shù)。這是行不通的。
我已經(jīng)為你安排好了一切!您可以使用onClick函數(shù)將您calculation
作為參數(shù)傳遞給您的函數(shù)operations
,并檢查傳遞的內(nèi)容switch
和case
此外,要獲取values
輸入的number
輸入,您需要使用parseInt()以便將輸入strings
轉(zhuǎn)換為數(shù)字format
。
此外,您需要確保在值中輸入了值,否則將顯示點擊功能NaN
。為避免這種情況,我們可以使用isNan函數(shù)來確保有一個value
for 計算。如果沒有值,您可以只顯示一條消息說請輸入一個值。
完整的工作演示:
var results = document.getElementById('results');
function operations(value) {
var numero_1 = parseInt(document.getElementById('num1').value);
var numero_2 = parseInt(document.getElementById('num2').value);
if (!isNaN(numero_1) && !isNaN(numero_2)) {
switch (value) {
case 'Sumar':
results.innerHTML = 'Results: ' + (numero_1 + numero_2);
break;
case 'Restar':
results.innerHTML = 'Results: ' + (numero_1 - numero_2);
break;
case 'Multiplicar':
results.innerHTML = 'Results: ' + (numero_1 * numero_2);
break;
case 'Dividir':
results.innerHTML = 'Results: ' + (numero_1 / numero_2);
break;
}
var final = document.getElementById('results').textContent;
console.log(final)
} else {
results.innerHTML = 'Please enter value to calculate'
}
}
<!DOCTYPE html>
<html dir="ltr">
<head>
<meta charset="utf-8">
<title> Calculadora </title>
</head>
<body>
<p>
<input type="number" id="num1" required />
<input type="number" id="num2" required/>
</p>
<p> <strong> ?Qué operación desea hacer? </strong> </p>
<p>
<input type="button" onclick="operations(this.value)" value="Sumar" id="suma" />
<input type="button" onclick="operations(this.value)" value="Restar" id="resta" />
<input type="button" onclick="operations(this.value)" value="Multiplicar" id="multiplicacion" />
<input type="button" onclick="operations(this.value)" value="Dividir" id="division" />
</p>
<div id="results"></div>
</body>
</html>
添加回答
舉報