设计网页,用JavaScript实现: 表单中,有可以输入数据的两个文本框,点击按钮,自动将输入的数值相加、相减、相乘、相除,并显示结果
时间: 2024-05-20 16:16:52 浏览: 81
<!DOCTYPE html>
<html>
<head>
<title>Calculator</title>
<script>
function calculate() {
var num1 = parseFloat(document.getElementById("num1").value);
var num2 = parseFloat(document.getElementById("num2").value);
var resultAdd = num1 + num2;
var resultSub = num1 - num2;
var resultMul = num1 * num2;
var resultDiv = num1 / num2;
document.getElementById("resultAdd").innerHTML = "Addition: " + resultAdd;
document.getElementById("resultSub").innerHTML = "Subtraction: " + resultSub;
document.getElementById("resultMul").innerHTML = "Multiplication: " + resultMul;
document.getElementById("resultDiv").innerHTML = "Division: " + resultDiv;
}
</script>
</head>
<body>
<form>
<label for="num1">Number 1:</label>
<input type="text" id="num1" name="num1"><br><br>
<label for="num2">Number 2:</label>
<input type="text" id="num2" name="num2"><br><br>
<button type="button" onclick="calculate()">Calculate</button>
<br><br>
<span id="resultAdd"></span><br>
<span id="resultSub"></span><br>
<span id="resultMul"></span><br>
<span id="resultDiv"></span><br>
</form>
</body>
</html>
阅读全文