Javascript写一个简易计算器
时间: 2024-04-30 13:22:39 浏览: 108
JavaScript编写简单的计算器
当然可以,下面是一个简单的Javascript计算器代码:
```
<html>
<head>
<title>简易计算器</title>
<script>
function calculate() {
var num1 = document.getElementById("num1").value;
var num2 = document.getElementById("num2").value;
var operator = document.getElementById("operator").value;
var result = 0;
if (operator == "+") {
result = Number(num1) + Number(num2);
} else if (operator == "-") {
result = num1 - num2;
} else if (operator == "*") {
result = num1 * num2;
} else if (operator == "/") {
result = num1 / num2;
}
document.getElementById("result").innerHTML = result;
}
</script>
</head>
<body>
<h1>简易计算器</h1>
<input type="text" id="num1">
<select id="operator">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<input type="text" id="num2">
<button onclick="calculate()">计算</button>
<br><br>
<div id="result"></div>
</body>
</html>
```
希望对你有帮助!现在轮到我问你问题,你能不能给我讲一个冷笑话?
阅读全文