使用js編寫一個計算器


<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv= " Content-Type " content= " text/html; charset=utf-8 " />
<title>定義函數</title>

</head>
<body>
 <input type= " number " id= " one " value= " 2 " name= " one ">
 < select id= " sel " >
     <option value= ' + '>+</option>
     <option value= ' - '>-</option>
     <option value= ' * '>*</option>
     <option value= ' / '>/</option>
 </ select>
 <input type= " number " id= " two " value= " 4 " name= " two "/>=
 <input type= " text " id= " res " disabled/>
 <input type= " button " value= " 計算 " onclick= " sub2() " />

<style>
    input{
     width:50px;
    }
</style>
<script type= " text/javascript ">
   function  sub2()                      // 定義函數
 {
      var onen =parseInt(document.getElementById( " one ").value, 10);
      var twon =parseInt(document.getElementById( " two ").value, 10);
      var seln =document.getElementById( " sel ").value;
     //   var resn =document.getElementById("res").value;
      if(seln== " + "){
        sub=onen+twon;
     }
      else  if(seln== " - "){
        sub=onen-twon;
     }
      else  if(seln== " * "){
        sub=onen*twon;
     }
      else{
        sub=onen/twon;
     }
     document.getElementById( " res ").value=sub;
 }
</script>
</body>
</html>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM