Rumah  >  Artikel  >  hujung hadapan web  >  js代码实现计算器

js代码实现计算器

零到壹度
零到壹度asal
2018-04-21 10:31:523858semak imbas

这篇文章介绍的内容是关于js代码实现计算器,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下

<!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>


相关推荐:

简单的计算器

html5计算器

计算器源代码

Atas ialah kandungan terperinci js代码实现计算器. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn