本文实例讲述了JavaScript实现简单的四则运算计算器。分享给大家供大家参考,具体如下:
运行效果图如下:
完整实例代码如下:
computerfunction compute(){
str1=Number(document.getElementById("txt1").value);
str2=Number(document.getElementById("txt2").value);
comp=document.getElementById("select").value;
var result;
switch(comp) {
case "+":
comp=str1+str2;
break;
case "-":
comp=str1-str2;
break;
case "*":
comp=str1*str2;
break;
case "/":
if(str2==0){
alert("除数不能为0!");
comp='';
}else{
comp=str1/str2;
}
break;
}
document.getElementById("result").value=comp;
}
+
-
*
/
PS:这里再为大家推荐几款计算工具供大家进一步参考借鉴:
在线一元函数(方程)求解计算工具:http://tools.uoften.com/jisuanqi/equ_jisuanqi
科学计算器在线使用_高级计算器在线计算:http://tools.uoften.com/jisuanqi/jsqkexue
在线计算器_标准计算器:http://tools.uoften.com/jisuanqi/jsq
更多关于JavaScript相关内容感兴趣的读者可查看本站专题:《JavaScript数学运算用法总结》、《JavaScript数据结构与算法技巧总结》、《JavaScript数组操作技巧总结》、《JavaScript排序算法总结》、《JavaScript遍历算法与技巧总结》、《JavaScript查找算法技巧总结》及《JavaScript错误与调试技巧总结》
希望本文所述对大家JavaScript程序设计有所帮助。