您的位置:首页 > Web前端 > JavaScript

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>

注意:数字中出现小数点能,可能会有误差!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: