JavaScript实现计算器 功能

代码实现:

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>js从入门到放弃</title>
</head>
<body>
<input type="text" id="txt" />
<select id="fuhao">
	<option>+</option>
	<option>-</option>
	<option>*</option>
	<option>/</option>
</select>
<input type="text" id="txt1" />
<input type="button" value="=" onclick="return jisuan()" />
<input type='text' id='txt2' >
<script type="text/javascript">
function jisuan()
{
	var txt=document.getElementById('txt').value;
	var txt1=document.getElementById('txt1').value;
	var txt2=document.getElementById('txt2');
	var fuhao=document.getElementById('fuhao').value;
	if(fuhao=='+')
	{
		var sum=parseInt(txt)+parseInt(txt1);
		txt2.value=sum;
	}
	else if(fuhao=='-')
	{
		var sum=parseInt(txt)-parseInt(txt1);
		txt2.value=sum;
	}
	else if(fuhao=='*')
	{
		var sum=parseInt(txt)*parseInt(txt1);
		txt2.value=sum;
	}
	else
	{
		var sum=parseInt(txt)/parseInt(txt1);
		txt2.value=sum;
	}
}
</script>
</body>
</html>

简单方法:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>a.html</title>
</head>
<body>
<script>
function fun(){
	var one=document.getElementById('one').value;
	var two=document.getElementById('two').value;
	var fuhao=document.getElementById('fuhao').value;
	var sum=eval(one+fuhao+two);
	document.getElementById('sum').value=sum;
}
</script>
<input type="text" id='one' />
<select id='fuhao'>
	<option value='+'>+</option>
	<option value='-'>-</option>
	<option value='*'>*</option>
	<option value='/'>/</option>
</select>
<input type='text' id='two' />
<input type='button' value='=' onclick='return fun()' />
<input type='text' id='sum' />
</body>
</html>

实现效果:

在这里插入图片描述

原文:https://blog.csdn.net/qq_42249896/article/details/82227426

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值