js实现一个简易计算器
作者:Pop_Rain 时间:2024-04-25 13:13:38
本文实例为大家分享了JS实现简易计算器的具体代码,供大家参考,具体内容如下
<!DOCTYPE html>
<html>
<head>
<title>14th_test</title>
<meta charset="gb2312">
</head>
<body>
<h1>这是一个标题</h1>
<p>以下是一个简易计算器</p>
<table border="1" style="position:center;">
<tr>
<th>第一个数</th>
<td><input id="fir" type="text" /></td>
</tr>
<tr>
<th>第二个数</th>
<td><input id="sec" type="text" /></td>
</tr>
<tr>
<td colspan="2">
<button type="button" οnclick="add()">+</button>
<button type="button" οnclick="substract()">-</button>
<button type="button" οnclick="multiply()">*</button>
<button type="button" οnclick="divide()">/</button>
</td>
</tr>
<tr>
<td colspan="2"><p id="result"></p></td>
</tr>
</table>
<script>
var res; //保存计算结果
function add()
{
var first = document.getElementById("fir").value;
var second = document.getElementById("sec").value;
res = Number(first) + Number(second); //这里"+"两端必须Number类型转换,否则当成字符串连接
sent(res);
}
function substract()
{
var first = document.getElementById("fir").value;
var second = document.getElementById("sec").value;
res = first - second;
sent(res);
}
function multiply()
{
var first = document.getElementById("fir").value;
var second = document.getElementById("sec").value;
res = first * second;
sent(res);
}
function divide()
{
var first = document.getElementById("fir").value;
var second = document.getElementById("sec").value;
res = first / second;
sent(res);
}
function sent(re)
{
document.getElementById("result").innerHTML = re;
}
</script>
</body>
</html>
output:
更多计算器功能实现,请点击专题: 计算器功能汇总 进行学习
来源:https://blog.csdn.net/Pop_Rain/article/details/74729294
标签:js,计算器
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python运行其他程序的实现方法
2022-11-15 05:18:09
Python演化计算基准函数详解
2021-02-13 19:55:32
![](https://img.aspxhome.com/file/2023/2/115882_0s.jpg)
Python的Flask框架应用调用Redis队列数据的方法
2023-04-20 14:14:38
史上最详细的Python打包成exe文件教程
2021-07-19 06:30:16
![](https://img.aspxhome.com/file/2023/9/108039_0s.png)
python基础教程之元组操作使用详解
2023-11-29 01:18:52
Python读取xlsx文件报错:xlrd.biffh.XLRDError: Excel xlsx file;not supported问题解决
2021-02-02 16:08:59
![](https://img.aspxhome.com/file/2023/2/72192_0s.png)
SqlServer字符截取的具体函数使用
2024-01-13 17:43:00
python3 wechatpy微信支付的项目实践
2023-08-29 14:13:36
![](https://img.aspxhome.com/file/2023/5/87975_0s.jpg)
Centos5.x下升级python到python2.7版本教程
2023-04-15 13:50:07
解决Golang time.Parse和time.Format的时区问题
2024-05-22 17:46:06
使用Python开发游戏运行脚本成功调用大漠插件
2021-03-09 21:05:53
![](https://img.aspxhome.com/file/2023/9/99679_0s.png)
js和php邮箱地址验证的实现方法
2024-06-05 09:38:13
django中F与Q查询的使用
2022-11-20 19:23:22
![](https://img.aspxhome.com/file/2023/3/93483_0s.png)
MySQL存储过程的查询命令介绍
2024-01-13 22:34:49
swiper在vue项目中loop循环轮播失效的解决方法
2024-05-03 15:10:24
基于Python编写简易版的天天跑酷游戏的示例代码
2023-10-23 01:10:11
![](https://img.aspxhome.com/file/2023/5/99405_0s.jpg)
python分割一个文本为多个文本的方法
2022-09-01 06:40:33
matplotlib之pyplot模块坐标轴标签设置使用(xlabel()、ylabel())
2021-10-16 11:01:10
![](https://img.aspxhome.com/file/2023/5/76185_0s.png)
python中map()函数使用方法详解
2022-12-19 13:56:37
![](https://img.aspxhome.com/file/2023/6/115676_0s.png)
Javascript中的isNaN函数使用说明
2023-08-27 10:10:02