javascript - 计算器未在控制台 HTML/JS 上显示结果
问题描述
我正在用 HTML/JS 制作一个简单而不同的计算器,但我不知道为什么它没有出现在控制台上。
我已经尝试了不同的东西,但没有。我是新来的。代码是西班牙语/英语
function operations()
{
var suma = document.getElementById('suma');
var resta = document.getElementById('resta');
var multiplicacion = document.getElementById('multiplicacion');
var division = document.getElementById('division');
var numero_1 = document.getElementById('num1');
var numero_2 = document.getElementById('num2');
switch (op.value) {
case suma.value:
return numero_1 + numero_2;
break;
case resta.value:
return numero_1 - numero_2;
break;
case multiplicacion.value:
return numero_1 * numero_2;
break;
case division.value:
return numero_1 / numero_2;
break;
console.log(operations());
}
}
<!DOCTYPE html>
<html lang="en" 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>
<script src="script.js"> </script>
</body>
</html>
解决方案
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 lang="en" 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>
推荐阅读
- vmware - 带有 vsphere 的 Packer 等待 IP 超时
- rust - 如何在 rust 的 GTK 事件中设置结构的值?
- python - Python -> 将数字从 0.0002321 转换为 2321
- ruby-on-rails - 更漂亮的 YAML - SyntaxError:紧凑映射中不允许嵌套映射
- java - 应用程序启动方法中的异常,javaFx 11
- python - Telethon:无法以编程方式开始会话
- date - Powerquery - 编辑器强制使用美国日期格式,包括源数据为英国日期格式时的参数
- html - 布局=固定时无法更改列宽和边距
- python-3.x - Google Drive api3 删除文件夹
- python - 打印 [Python] 在 Pycharm 中被视为关键字