js编写一个简易计算器
时间: 2024-02-23 19:34:00 浏览: 32
好的,我可以回答这个问题。以下是一个简易的计算器的JavaScript代码:
```
<html>
<head>
<title>简易计算器</title>
<script type="text/javascript">
function calculate() {
var num1 = parseInt(document.getElementById("num1").value);
var num2 = parseInt(document.getElementById("num2").value);
var operator = document.getElementById("operator").value;
var result;
if (operator == "+") {
result = num1 + num2;
}
else if (operator == "-") {
result = num1 - num2;
}
else if (operator == "*") {
result = num1 * num2;
}
else if (operator == "/") {
result = num1 / num2;
}
document.getElementById("result").value = result;
}
</script>
</head>
<body>
<h1>简易计算器</h1>
<input type="text" id="num1" placeholder="请输入第一个数">
<select id="operator">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<input type="text" id="num2" placeholder="请输入第二个数">
<button onclick="calculate()">计算</button>
<br>
<input type="text" id="result" placeholder="计算结果">
</body>
</html>
```
希望这个代码可以帮助你实现一个简易的计算器。如果你有其他问题,请继续问我。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)