实现计算器HTML+JS简单实现计算器

这个是在闲暇之余,写出玩的一个小东西,没有复杂的算法,没有特别厉害的逻辑。只是方便有人需要的话直接用。

也希望有大神可以帮我设计设计。


下面是代码:

   <!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<link rel="stylesheet" type="text/css" href="untitled.css">
</head>
<script type="text/javascript"> 
        var result="";
          function jisuan(num){
      if(num=="="){
  document.form1.text.value=eval(result);
  }else{
  result=result+num;
  document.form1.text.value=result;
  }
  }
</script>
<body>


<div class="box">
<form action="" class="form1" name="form1">
      <div class="header"> 
         <input type="text" id="text" name="text" class="text"/>
        
          </div>
          <div class="nav">
     <table class="table">
         <tr>
         <td>  <input id="1" style="height:55px; width:55px;" type="button" value="1" onClick="jisuan(this.id)"/> </td>
         <td>  <input id="2" style="height:55px; width:55px;" type="button" value="2" onClick="jisuan(this.id)"/> </td>
         <td>  <input id="3" style="height:55px; width:55px;" type="button" value="3" onClick="jisuan(this.id)"/> </td>
         <td>  <input id="+" style="height:55px; width:55px;" type="button" value="+" onClick="jisuan(this.id)"/> </td>
         </tr>
         <tr>
         <td>  <input id="4" style="height:55px; width:55px;" type="button" value="4" onClick="jisuan(this.id)"/> </td>
         <td>  <input id="5" style="height:55px; width:55px;" type="button" value="5" onClick="jisuan(this.id)"/> </td>
         <td>  <input id="6" style="height:55px; width:55px;" type="button" value="6" onClick="jisuan(this.id)"/> </td>
          <td>  <input id="-" style="height:55px; width:55px;" type="button" value="-" onClick="jisuan(this.id)"/> </td>
         </tr>
         <tr>
         <td>  <input id="7" style="height:55px; width:55px;" type="button" value="7" onClick="jisuan(this.id)"/> </td>
         <td>  <input id="8" style="height:55px; width:55px;" type="button" value="8" onClick="jisuan(this.id)"/> </td>
         <td>  <input id="9" style="height:55px; width:55px;" type="button" value="9" onClick="jisuan(this.id)"/> </td>
          <td>  <input id="*" style="height:55px; width:55px;" type="button" value="*" onClick="jisuan(this.id)"/> </td>
         </tr>
          <tr>
         <td>  <input id="/" style="height:55px; width:55px;" type="button" value="/" onClick="jisuan(this.id)"/> </td>
         <td>  <input id="0" style="height:55px; width:55px;" type="button" value="0" onClick="jisuan(this.id)"/> </td>
          <td>  <input id="." style="height:55px; width:55px;" type="button" value="." onClick="jisuan(this.id)"/></td>
          <td>  <input id="=" style="height:55px; width:55px;" type="button" value="=" onClick="jisuan(this.id)"/> </td>
         </tr>
         </table>
        
         
     </div>
   </form>
 </div>








</body>
</html>


  • 16
    点赞
  • 59
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
好的,以下是一个简单HTMLJavaScript实现计算器的示例: ```html <!DOCTYPE html> <html> <head> <title>计算器</title> </head> <body> <input type="text" id="input" disabled><br> <button onclick="clearInput()">清空</button> <button onclick="backspace()">退格</button><br> <button onclick="appendToInput('7')">7</button> <button onclick="appendToInput('8')">8</button> <button onclick="appendToInput('9')">9</button> <button onclick="appendToInput('/')">÷</button><br> <button onclick="appendToInput('4')">4</button> <button onclick="appendToInput('5')">5</button> <button onclick="appendToInput('6')">6</button> <button onclick="appendToInput('*')">×</button><br> <button onclick="appendToInput('1')">1</button> <button onclick="appendToInput('2')">2</button> <button onclick="appendToInput('3')">3</button> <button onclick="appendToInput('-')">-</button><br> <button onclick="appendToInput('0')">0</button> <button onclick="appendToInput('.')">.</button> <button onclick="calculate()">=</button> <button onclick="appendToInput('+')">+</button><br> <script> function appendToInput(val) { document.getElementById('input').value += val; } function clearInput() { document.getElementById('input').value = ''; } function backspace() { var input = document.getElementById('input').value; document.getElementById('input').value = input.substring(0, input.length - 1); } function calculate() { var input = document.getElementById('input').value; var result = eval(input); document.getElementById('input').value = result; } </script> </body> </html> ``` 这个计算器的界面由一些按钮和一个输入框组成,按钮被点击时会调用对应的JavaScript函数。`appendToInput`函数用于将按钮的值添加到输入框中,`clearInput`函数用于清空输入框,`backspace`函数用于删除输入框中的最后一个字符,`calculate`函数用于计算输入框中的表达式并将结果显示在输入框中。注意,这个计算器的计算方法使用了JavaScript内置的`eval`函数,这个函数可以计算字符串表达式的值,但是需要注意安全问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值