Home > Web Front-end > JS Tutorial > Simple js calculator implementation

Simple js calculator implementation

高洛峰
Release: 2016-12-08 17:13:15
Original
1260 people have browsed it

The example in this article shares the js calculator implementation code for your reference. The specific content is as follows

<!DOCTYPE html>
 
<html>
 
 <head>
 
  <meta charset="utf-8"> 
 
  <title></title>
 
  </head>
 
  <body >
 
   <input type=&#39;text&#39; id=&#39;txt1&#39; />
 
   <select id=&#39;select&#39;>      
 
      <option value=&#39;+&#39;>+</option>
 
      <option value="-">-</option>
 
      <option value="*">*</option>
 
      <option value="/">/</option>
 
   </select>
 
   <input type=&#39;text&#39; id=&#39;txt2&#39; />
 
   <input type=&#39;button&#39; value=&#39; = &#39; onclick="count()"/>
 
   <!--通过 = 按钮来调用创建的函数,得到结果-->
 
   <input type=&#39;text&#39; id=&#39;fruit&#39; />
 
 </body>
 
  <script type="text/javascript">
 
   //制做简单计算器
 
     function count() {
 
      var txt1 = document.all.txt1.value;
 
      var txt2 = document.all.txt2.value;
 
      var s=document.all.select.value;
 
      var z;
 
      switch(s){
 
       case "+":                    z=parseFloat(txt1)+parseFloat(txt2);break;
 
      case "-":
 
          z=parseFloat(txt1)-parseFloat(txt2);break;
 
       case                  "*":z=parseFloat(txt1)*parseFloat(txt2);break;
 
      case         "/":z=parseFloat(txt1)/parseFloat(txt2);break;
 
  
 
  
 
        }
 
        document.getElementById("fruit").value=z;
 
  
 
       }
 
     </script>
 
  
 
</html>
Copy after login


Related labels:
js
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