首頁  >  文章  >  web前端  >  如何用HTML+CSS+JavaScript製作簡易計算器

如何用HTML+CSS+JavaScript製作簡易計算器

yulia
yulia原創
2018-10-24 11:31:2612667瀏覽

JavaScript是前端開發中不可或缺的一部分,因為頁面功能的實作離不開JS,身為前端開發人員,你會用JavaScript製作一個簡易計算器嗎?這篇文章就跟大家講講如何用HTML、CSS、JavaScript製作一個簡易計算器,並且能夠實現加減乘除的功能,有一定的參考價值,有興趣的朋友可以看看。

製作一個簡單的計算機需要用到很多CSS屬性、HTML標籤和JavaScript知識,如有不清楚的同學可以參考PHP中文網的相關文章,或者訪問 JavaScript視頻教程 ,希望對你有幫助。

實例描述:用HTML和CSS建立頁面,用JavaScript實現加減乘除的功能,當點擊清除時,輸入框沒有值,當點擊「=」時,顯示計算結果,具體程式碼如下:

HTML部分:

<div class="center">
   <h1>计算器</h1>   
   <form name="calculator">
    <input type="button" id="clear" class="btn other" value="C">
    <input type="text" id="display">
     <br>
    <input type="button" class="btn number" value="7" onclick="get(this.value);">
    <input type="button" class="btn number" value="8" onclick="get(this.value);">
    <input type="button" class="btn number" value="9" onclick="get(this.value);">
    <input type="button" class="btn operator" value="+" onclick="get(this.value);">
     <br>
    <input type="button" class="btn number" value="4" onclick="get(this.value);">
    <input type="button" class="btn number" value="5" onclick="get(this.value);">
    <input type="button" class="btn number" value="6" onclick="get(this.value);">
    <input type="button" class="btn operator" value="*" onclick="get(this.value);">
     <br>
    <input type="button" class="btn number" value="1" onclick="get(this.value);">
    <input type="button" class="btn number" value="2" onclick="get(this.value);">
    <input type="button" class="btn number" value="3" onclick="get(this.value);">
    <input type="button" class="btn operator" value="-" onclick="get(this.value);">
     <br>
    <input type="button" class="btn number" value="0" onclick="get(this.value);">
    <input type="button" class="btn operator" value="." onclick="get(this.value);">
    <input type="button" class="btn operator" value="/" onclick="get(this.value);">   
    <input type="button" class="btn other" value="=" onclick="calculates();">
   </form>
  </div>

CSS部分:

* {
    border: none;
    font-family: &#39;Open Sans&#39;, sans-serif;
    margin: 0;
    padding: 0;
   }   
   .center {
    background-color: #fff;
    border-radius: 50%;
    height: 600px;
    margin: auto;
    width: 600px;
   }
   h1 {
    color: #495678;
    font-size: 30px; 
    margin-top: 20px;
    padding-top: 50px;
    display: block;
    text-align: center;
   }   
   form {
    background-color: #495678;
    margin: 40px auto;
    padding: 40px 0 30px 40px; 
    width: 280px;
   }
   .btn {
    outline: none;
    cursor: pointer;
    font-size: 20px;
    height: 45px;
    margin: 5px 0 5px 10px;
    width: 45px;
   }
   .btn:first-child {
    margin: 5px 0 5px 10px;
   }
   .btn, #display, form {
    border-radius: 25px;
   }
   #display {
    outline: none;
    background-color: #98d1dc;
    color: #dededc;
    font-size: 20px;
    height: 47px;
    text-align: right;
    width: 165px;
    padding-right: 10px;
    margin-left: 10px;
   }
   .number {
    background-color: #72778b;
    color: #dededc;
   }
   .number:active {
      -webkit-transform: translateY(2px);
      -ms-transform: translateY(2px);
      -moz-tranform: translateY(2px);
      transform: translateY(2px);
   }
   .operator {
    background-color: #dededc;
    color: #72778b;
   }
   .operator:active {
      -webkit-transform: translateY(2px);
      -ms-transform: translateY(2px);
      -moz-tranform: translateY(2px);
      transform: translateY(2px);
   }
   .other {
    background-color: #e3844c;
    color: #dededc;
   }
   .other:active {
      -webkit-transform: translateY(2px);
      -ms-transform: translateY(2px);
      -moz-tranform: translateY(2px);
      transform: translateY(2px);
   }

JavaScript部分:

/* limpa o display */ 
  document.getElementById("clear").addEventListener("click", function() {
   document.getElementById("display").value = "";
  });
  /* recebe os valores */
  function get(value) {
   document.getElementById("display").value += value; 
  }   
  /* calcula */
  function calculates() {
   var result = 0;
   result = document.getElementById("display").value;
   document.getElementById("display").value = "";
   document.getElementById("display").value = eval(result);
  };

效果如圖所示:

如何用HTML+CSS+JavaScript製作簡易計算器

以上給大家分享了HTML、CSS和JavaScript製作簡易計算器的程式碼,對於初學者來說可能有一定的難度,不用擔心,把基礎知識學紮實以後,相信你很容易理解的,希望這篇文章對你有幫助!

【相關教學推薦】

1. JavaScript中文參考手冊
2. CSS3教學
3. # bootstrap教程

以上是如何用HTML+CSS+JavaScript製作簡易計算器的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn