Home > Web Front-end > JS Tutorial > body text

js code to implement calculator

零到壹度
Release: 2018-04-21 10:31:52
Original
3863 people have browsed it

The content of this article is about the js code implementation of the calculator. It has a certain reference value. Now I share it with you. Friends in need can refer to it

<!DOCTYPE html>  
<html>  
<head>  
<meta charset="utf-8">   
<title></title>   
<script>  
  
function myjs(){  
       
    var ft =  document.getElementById("fhv").value;  
    var v1 =  document.getElementById("v1").value;  
    var v2 =  document.getElementById("v2").value;  
     var v3 = &#39;&#39;;  
     //alert(ft);  
     //alert(v1);  
     //alert(v2);  
     //alert(v3);  
    if("+" == ft){  
         v3 =  Number(v1) +Number(v2);  
    }else if("-" == ft){  
         v3 = Number(v1)-Number(v2);  
    }else if("*" == ft){  
         v3 = Number(v1)*Number(v2);  
    }else if("/" == ft){  
         v3 = Number(v1)/Number(v2);  
    }else{  
        v3 = &#39;&#39;;   
    }  
     //alert(v3);  
    document.getElementById("v3").value=  v3;  
    document.getElementById("z3").innerHTML = v3;  
      
}  
function changeFh(){  
    var fu =  document.getElementById("fh");  
    var idx= fu.selectedIndex ;     
    var ft =  fu.options[idx].value;  
    var fs =  fu.options[idx].text;  
    //alert(ft == "+");  
    if("+" == ft){  
        document.getElementById("an").innerHTML=  &#39;加&#39;;  
        document.getElementById("fhv").value=  &#39;+&#39;;  
    }else if("-" == ft){  
        document.getElementById("an").innerHTML=  &#39;减&#39;;  
        document.getElementById("fhv").value=  &#39;-&#39;;  
    }else if("*" == ft){  
        document.getElementById("an").innerHTML=  &#39;乘&#39;;  
        document.getElementById("fhv").value=  &#39;*&#39;;  
    }else if("/" == ft){  
        document.getElementById("an").innerHTML=  &#39;除&#39;;  
        document.getElementById("fhv").value=  &#39;/&#39;;  
    }else{  
        document.getElementById("an").innerHTML=  &#39;请选择符号&#39;;  
        document.getElementById("fhv").value=  &#39;&#39;;  
    }  
    var v1 = document.getElementById("v1").value;  
    v1 = v1.replace(/\s+/g,"");  
   
    if(v1.length == 0){  
        document.getElementById("fhz").innerHTML = &#39;&#39;;   
    }  
  
    }  
    function radow_load(){  
        changeFh();  
    }  
    function checkValue1(){  
        var v1 = document.getElementById("v1").value;  
        v1 = v1.replace(/\s+/g,"");  
        //alert(isNaN(v1));  
        if(isNaN(v1)){  
            document.getElementById("v1").value = ""    ;  
            alert("请输入数字!");  
        }  
        document.getElementById("z1").innerHTML = v1;   
    }     
    function checkValue2(){  
        var v2 = document.getElementById("v2").value;  
        v2 = v2.replace(/\s+/g,"");  
        if(isNaN(v2)){  
            document.getElementById("v2").value = &#39;&#39;;  
            alert("请输入数字");  
        }  
        var fhx = document.getElementById("fhv").value;  
        if(fhx.length == 0){  
            document.getElementById("v2").value = &#39;&#39;;  
            alert("请输入符号");  
            return;  
        }  
        document.getElementById("fhz").innerHTML = fhx;   
        document.getElementById("z2").innerHTML = v2;   
        document.getElementById("fhzy").innerHTML = &#39;=&#39;;   
    }     
    function clearValue(){  
        //alert("clear");  
        document.getElementById("v1").value=  &#39;&#39;;  
        document.getElementById("v2").value=  &#39;&#39;;  
        document.getElementById("v3").value=  &#39;&#39;;  
        document.getElementById("fhv").value=  &#39;&#39;;  
        document.getElementById("fh").value=  &#39;&#39;;  
        document.getElementById("fhz").value=  &#39;&#39;;  
        document.getElementById("an").innerHTML=  &#39;请选择符号&#39;;  
        document.getElementById("z1").innerHTML=  &#39;&#39;;  
        document.getElementById("z2").innerHTML=  &#39;&#39;;  
        document.getElementById("z3").innerHTML=  &#39;&#39;;  
        document.getElementById("fhzy").innerHTML=  &#39;&#39;;  
        document.getElementById("fhz").innerHTML=  &#39;&#39;;  
  
    }  
</script>  
</head>  
<body onload="radow_load()">  
      
<h1>我的 Web 页面</h1>  
<table>  
<tr>  
<td>  
<input id="v1" name="第一个值"   onchange="checkValue1()"/>  
<select id="fh" name="符号" onchange="changeFh()" >  
    <option value ="+">+</option>  
    <option value ="-">-</option>  
    <option value="*">*</option>  
    <option value="/">/</option>  
</select>  
<input id="v2"  name="第二个值" onchange="checkValue2()"/>  
<b>=</b>  
<input id="v3"  name="结果"/>  
 <button id="an" type="button" onclick="myjs()">请选择符号</button>  
 <button id="ca" type="button" onclick="clearValue()">清空</button>  
<input id="fhv"  name="fhv" hidden="true"/>  
</td>  
</tr>  
<tr>  
<td>  
<b id="z1"></b>  
<b id="fhz"></b>  
<b id="z2"></b>  
<b id="fhzy"></b>  
<b id="z3"></b>  
</td>  
  
</tr>  
  
</table>  
</body>  
</html>
Copy after login


Related recommendations:

Simple calculator

html5 calculator

Calculator source code

The above is the detailed content of js code to implement calculator. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template