[java代码库]-简易计算器(第二种)

  第二种方案:在程序中不使用if/switch……case等语句,完成计算器功能。

<html>
	<head>
		<title>简易计算器</title>
		<script language="javascript">//易错:不是"text/javascript"
			function doCal(){
				var value1=parseInt(document.getElementById("value1").value);//易错:易错:getElementById首字母大写,不是byId				
				var flag=document.getElementById("flag").value;
				var value2=parseInt(document.getElementById("value2").value);//易错:getElementBy返回是String类型,应该通过parseInt转换为Int类型
				var s=0;				
				eval("var s="+value1+flag+value2);//eval把string类型转换为表达式进行计算
				//使运算结果显示在浏览器中,注意=右侧为string类型
				document.getElementById("span_result").innerHTML="<font size='16' color='red'>"+s+"</font>"
			}								
		</script>
	</head>
	<body>
		<h1>简易计算器</h1>
		<hr>
		<input type="text" name="value1" >
		<select name="flag" ><!--注意select-option的用法-->
			<option value="+">+</option>
			<option value="-">-</option>
			<option value="*">*</option>
			<option value="/">/</option>
		</select>
		
		<input type="text" name="value2" >
		<input type="button" value="=" onclick="doCal()"><!--button的onclick事件-->
		<span ></span><!--通过span显示结果-->
	</body>
</html>

  

相关文章:

  • 2021-05-27
  • 2022-02-14
  • 2022-02-09
  • 2022-12-23
  • 2022-12-23
  • 2021-11-05
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-11-28
  • 2021-12-24
  • 2021-12-30
  • 2022-12-23
  • 2021-11-21
相关资源
相似解决方案