>  기사  >  웹 프론트엔드  >  javascript_javascript 스킬을 기반으로 간단한 계산기 기능 구현

javascript_javascript 스킬을 기반으로 간단한 계산기 기능 구현

WBOY
WBOY원래의
2016-05-16 15:22:151126검색

본 글의 예시에서는 간단한 계산기 기능을 구현하기 위한 자바스크립트의 상세한 코드를 소개하고 있으며, 참고하실 수 있도록 공유드립니다

렌더링:

구현 코드:

<html> 
  <head> 
    <script> 
      function calc(event){ 
        // test  
        //window.alert(event.value); 
        var val = new String(event.value); 
        // clear space 
        val = val.trim(); 
        var res = document.getElementById("res"); 
        // clear 
        if(val == "clear"){ 
          res.value = ""; 
        } 
 
        // back 
        if(val == "back"){ 
          res.value = res.value.substring(0, res.value.length - 1); 
        } 
 
        // power 
        if(val == "power"){ 
          val = "p"; 
        } 
        // add val to text 
        if(val.length == 1 && val != "="){ 
          res.value = res.value + val; 
        } 
  
        // calc result 
        if(val == "="){ 
          var arr; 
          var result; 
          // power 
          if(res.value.indexOf("p") != -1){ 
            arr = res.value.split("p"); 
            //window.alert(arr); 
             result = Math.pow(parseFloat(arr[0]) ,parseFloat(arr[1])); 
            //window.alert(res); 
            res.value = result; 
          }       
          // plus 
          if(res.value.indexOf("+") != -1){ 
            arr = res.value.split("+"); 
            //window.alert(arr); 
             result = parseFloat(arr[0]) + parseFloat(arr[1]); 
            //window.alert(res); 
            res.value = result; 
          } else if(res.value.indexOf("-") != -1){ 
            // minus 
            arr = res.value.split("-"); 
            //window.alert(arr); 
            result = parseFloat(arr[0]) - parseFloat(arr[1]); 
            //window.alert(res); 
            res.value = result; 
          } else if(res.value.indexOf("*") != -1){ 
            // multiply 
            arr = res.value.split("*"); 
            //window.alert(arr); 
            result = parseFloat(arr[0]) * parseFloat(arr[1]); 
            //window.alert(res); 
            res.value = result; 
          } else if(res.value.indexOf("/") != -1){ 
            // division 
            arr = res.value.split("/"); 
            //window.alert(arr); 
            result = parseFloat(arr[0]) / parseFloat(arr[1]); 
            //window.alert(res); 
            res.value = result; 
          } else if(res.value.indexOf("%") != -1){ 
            // module 
            arr = res.value.split("%"); 
            //window.alert(arr); 
            result = parseFloat(arr[0]) % parseFloat(arr[1]); 
            //window.alert(res); 
            res.value = result; 
          } 
        }   
      } 
    </script> 
  </head> 
  <body> 
    <table border="1px" cellpadding="10px" cellspacing="5px" align="center"> 
      <tr align="center"> 
        <td colspan="4"><input type="text" id="res" size="35px" value="" style="text-align:right;"/></td> 
      </tr> 
      <tr align="center"> 
        <td><input type="button" value="power" onclick="calc(this)"/></td> 
        <td><input type="button" value="clear" onclick="calc(this)"/></td> 
        <td colspan="2"><input type="button" value="   back   " onclick="calc(this)"/></td> 
      </tr> 
      <tr align="center"> 
        <td><input type="button" value="  1  " onclick="calc(this)"/></td> 
        <td><input type="button" value="  2  " onclick="calc(this)"/></td> 
        <td><input type="button" value="  3  " onclick="calc(this)"/></td> 
        <td><input type="button" value="  +  " onclick="calc(this)"/></td> 
      </tr> 
      <tr align="center"> 
        <td><input type="button" value="  4  " onclick="calc(this)"/></td> 
        <td><input type="button" value="  5  " onclick="calc(this)"/></td> 
        <td><input type="button" value="  6  " onclick="calc(this)"/></td> 
        <td><input type="button" value="  -  " onclick="calc(this)"/></td> 
      </tr> 
      <tr align="center"> 
        <td><input type="button" value="  7  " onclick="calc(this)"/></td> 
        <td><input type="button" value="  8  " onclick="calc(this)"/></td> 
        <td><input type="button" value="  9  " onclick="calc(this)"/></td> 
        <td><input type="button" value="  *  " onclick="calc(this)"/></td> 
      </tr> 
      <tr align="center"> 
        <td><input type="button" value="  0  " onclick="calc(this)"/></td> 
        <td><input type="button" value="  =  " onclick="calc(this)"/></td> 
        <td><input type="button" value="  %  " onclick="calc(this)"/></td> 
        <td><input type="button" value="  /  " onclick="calc(this)"/></td> 
      </tr> 
    </table> 
  </body> 
</html> 

이 기사가 JavaScript 프로그래밍을 배우는 모든 사람에게 도움이 되기를 바랍니다.

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.