网页版的计算器

<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://cdn.bootcss.com/html5shiv/r29/html5.js"></script>
    <meta charset="UTF-8">
    <title>My Calculator</title>
 <style type="text/css">
     body{
      background-repeat: no-repeat;
      background-position: center 3px;
    }
   .main{

      background-color: #d6fae3;
     text-align: center;
     border: papayawhip dashed 8px;
      margin: 0 auto;
      width: 700px;
      height: 970px;


    }
   .title{
     color: #b3d4fc;
      text-align: center;
      font-family: 华文彩云;
     font-size: 80px;
      margin: 0 auto;
      width: 700px;
      height: 100px;
   }

    .displayResult{
      text-align: center;

      margin: 0 auto;
      width: 700px;
      height: 200px;

    }
    #result{
      background-color: white;
      text-align:right;
      border: 1px solid black;
      font-weight: bolder;
       width: 700px;
      height: 190px;
    }
.key{
      margin: 0 auto;
      width: 700px;
      height: 670px;

}
    .tab{
      text-align: center;
      padding: 10px;
      width: 700px;
      height: 650px;

    }
    button{
      border-radius: 8px;
      font-size: 21px;
      width: 150px;
      height: 90px;
      background-color: #feff8a;
    }
.getResult{
      border-radius: 8px;
      font-size: 21px;
      width: 650px;
      height: 90px;
      background-color: #feff8a;
    }
#res{
  color: #6399ff;
  font-size: 48px;

}
#exp{
  color: black;
  font-size: 27px;
}



 </style>
  <script type="text/javascript">
    var arr=[];/*用一个数组保存要计算结果的字符串*/
       var reg=/,/g;  /*正则表达式用来全局搜索数组里面的逗号*/
        var s=0;   /*接收返回计算结果的字符串*/
    function  getKeys(key) {

          arr.push(key);
                     /*key传入的是什么,数组就添加什么字符*/
           var str= arr.toString().replace(reg,"");/*将字符串里面所有的逗号清空*/
         document.getElementById("exp").innerText=str+"";/*将表达式输出在文本域内并显示*/
              s=str;
         return s;
    }
        function getResult() {
         var rs= eval(s);    //eval()方法计算字符串的值
         document.getElementById("res").innerText=rs+"";
         arr=[];  /*计算完结果清空保存要计算结果的字符串*/
    }
    function removeAll() {
            /*清除显示的所有内容*/
            /*把存储键入字符串清空*/ /*清空input框里面所有内容*/
            document.getElementById("exp").innerText="";
              document.getElementById("res").innerText="";
               arr=[];
    }

  </script>
</head>
<body>
<div class="main">
  <div class="title">My Calculator
  </div>
  <div class="displayResult">
  <table id="result">
     <tr><td id="exp"></td></tr>
        <tr><td id="res"></td></tr>
  </table>
</div>
<div class="key">
  <table class="tab">
    <tr>
      <td><button  onclick="getKeys(7)">7</button></td>
       <td><button  onclick="getKeys(8)">8</button></td>
       <td><button onclick="getKeys(9)">9</button></td>
       <td><button  onclick="getKeys('+')">+</button></td>
    </tr>
   <tr>
      <td><button  onclick="getKeys(4)">4</button></td>
       <td><button onclick="getKeys(5)">5</button></td>
       <td><button  onclick="getKeys(6)">6</button></td>
       <td><button   onclick="getKeys('-')">-</button></td>
    </tr>
    <tr>
      <td><button  onclick="getKeys(1)">1</button></td>
       <td><button  onclick="getKeys(2)">2</button></td>
       <td><button onclick="getKeys(3)">3</button></td>
       <td><button onclick="getKeys('*')">*</button></td>
    </tr>
    <tr>
      <td><button onclick="getKeys(0)">0</button></td>
       <td><button onclick="getKeys('.')">.</button></td>
       <td><button onclick="removeAll()">AC</button></td>
       <td><button  onclick="getKeys('/')">/</button></td>
    </tr>
    <tr><td colspan="4"><button class="getResult" onclick="getResult()">=</button></td></tr>
  </table>
</div>
</div>
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的 HTML 网页版计算器的代码示例: ```html <!DOCTYPE html> <html> <head> <title>网页版计算器</title> <meta charset="UTF-8"> <style> input[type=button] { width: 50px; height: 50px; font-size: 20px; margin: 5px; } input[type=text] { width: 206px; height: 50px; font-size: 20px; margin: 5px; } </style> </head> <body> <h1>网页版计算器</h1> <input type="text" id="result" readonly><br> <input type="button" value="1" onclick="appendValue('1')"> <input type="button" value="2" onclick="appendValue('2')"> <input type="button" value="3" onclick="appendValue('3')"> <input type="button" value="+" onclick="appendValue('+')"><br> <input type="button" value="4" onclick="appendValue('4')"> <input type="button" value="5" onclick="appendValue('5')"> <input type="button" value="6" onclick="appendValue('6')"> <input type="button" value="-" onclick="appendValue('-')"><br> <input type="button" value="7" onclick="appendValue('7')"> <input type="button" value="8" onclick="appendValue('8')"> <input type="button" value="9" onclick="appendValue('9')"> <input type="button" value="*" onclick="appendValue('*')"><br> <input type="button" value="0" onclick="appendValue('0')"> <input type="button" value="." onclick="appendValue('.')"> <input type="button" value="C" onclick="clearResult()"> <input type="button" value="/" onclick="appendValue('/')"><br> <input type="button" value="=" onclick="calculate()"> <script> function appendValue(value) { document.getElementById("result").value += value; } function clearResult() { document.getElementById("result").value = ""; } function calculate() { var result = eval(document.getElementById("result").value); document.getElementById("result").value = result; } </script> </body> </html> ``` 该网页版计算器使用了 HTML、CSS 和 JavaScript 技术,包含一个文本框用于显示运算结果,以及若干个按钮用于输入数字和运算符。JavaScript 代码实现了输入内容的追加、清空、以及运算结果的计算和显示。注意,该计算器仅作为示例,不具备完整的计算器功能和安全性,仅供学习和参考。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值