溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

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

發(fā)布時間:2020-07-08 16:29:38 來源:億速云 閱讀:149 作者:Leah 欄目:web開發(fā)

HTML+CSS+JavaScript如何制作簡易計算器?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

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

HTML部分:

<div class="center">
   <h2>計算器</h2>   
   <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: 'Open Sans', sans-serif;
    margin: 0;
    padding: 0;
   }   
   .center {
    background-color: #fff;
    border-radius: 50%;
    height: 600px;
    margin: auto;
    width: 600px;
   }
   h2 {
    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如何制作簡易計算器問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業(yè)資訊頻道了解更多相關知識。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI