js实现简易计算器功能
2015-08-27 16:05
597 查看
<html>
<head>
<meta http-equiv="Content-type" charset="utf-8" type="text/html">
<title>计算器</title>
<script type="text/javascript">
function count(){
var itxt1=document.getElementById("txt1").value;
var itxt2=document.getElementById("txt2").value;
var test=document.getElementById("select").value;
var result="";
switch(test){
case "+":
result=parseFloat(itxt1)+parseFloat(itxt2);
break;
case "-":
result=parseFloat(itxt1)-parseFloat(itxt2);
break;
case "*":
result=parseFloat(itxt1)*parseFloat(itxt2);
break;
default:
result=parseFloat(itxt1)/ parseFloat(itxt2); }
document.getElementById("fruit").value=result;
}
</script>
</head>
<body>
<input type="text" id="txt1">
<select id="select">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<input type="text" id="txt2">
<input type="button" value='=' onclick="count()"/>
<input type="text" id="fruit">
</body>
</html>
注意:数字中出现小数点能,可能会有误差!
<head>
<meta http-equiv="Content-type" charset="utf-8" type="text/html">
<title>计算器</title>
<script type="text/javascript">
function count(){
var itxt1=document.getElementById("txt1").value;
var itxt2=document.getElementById("txt2").value;
var test=document.getElementById("select").value;
var result="";
switch(test){
case "+":
result=parseFloat(itxt1)+parseFloat(itxt2);
break;
case "-":
result=parseFloat(itxt1)-parseFloat(itxt2);
break;
case "*":
result=parseFloat(itxt1)*parseFloat(itxt2);
break;
default:
result=parseFloat(itxt1)/ parseFloat(itxt2); }
document.getElementById("fruit").value=result;
}
</script>
</head>
<body>
<input type="text" id="txt1">
<select id="select">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<input type="text" id="txt2">
<input type="button" value='=' onclick="count()"/>
<input type="text" id="fruit">
</body>
</html>
注意:数字中出现小数点能,可能会有误差!
相关文章推荐
- String to Date jstl标签的fmt:formatDate格式化日期
- seaJs使用记录
- JS学习之闭包的理解
- json格式字符串转集合
- 闭包-JS
- js 保持在不同区域鼠标动作不被中断
- 用js识别字符串中的英文字母字符和非字符(汉字)
- javascript常见的20个问题与解决方法
- JS执行流程
- 【JS/辨析】js中的计时器:setTimeout & setInterval
- JavaScript 对象的概述
- 基于zepto使用swipe.js制作轮播图demo
- javascript中window对象及属性
- js 页面刷新location.reload和location.replace的区别小结
- js里面字符1和数字1相加的结果是11,如何解决这个问题。js里面使用EL表达式。
- 笔记js+html+css
- Js解析xml文件并简单实现省市区级联菜单(并解决各浏览器兼容性问题).
- 第三篇 Arcgis api for js之dojo框架使用
- JavaScript实现非常简单实用的下拉菜单效果
- JavaScript中的Function函数