机试html+js简易计算器

<pre name="code" class="html"><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>demo-cal</title>
	<meta name="author" content="lenovo" />
	<style>
    * {
      margin:0;padding:0;
    }
    #txtContains {
      width:500px;
      height:80px;
      margin:0 auto;
    }
	  #txt {
	    width:475px;
	    height:60px;
	    font-size:35px;  
	  }
	  #btnContains {
	    width:500px;
	    margin:0 auto;
	  }
	  input[type=button] {
	    width:100px;
	    height:60px;
	    font-size:35px;
	    margin:0 auto;
	  }
	</style>
	<!-- Date: 2014-10-15 -->
</head>
<body>
  <div id="txtContains"><input id="txt" type="text" placeholder="cal"></div>
  <table id="btnContains">
    <tr>
      <td><input id="btn1" type="button" value="1" οnclick="showValue(this);"></td>
      <td><input id="btn2" type="button" value="2" οnclick="showValue(this);"></td>
      <td><input id="btn3" type="button" value="3" οnclick="showValue(this);"></td>
      <td><input id="btn+" type="button" value="+" οnclick="showValue(this);"></td>
    </tr>
    <tr>
      <td><input id="btn4" type="button" value="4" οnclick="showValue(this);"></td>
      <td><input id="btn5" type="button" value="5" οnclick="showValue(this);"></td>
      <td><input id="btn6" type="button" value="6" οnclick="showValue(this);"></td>
      <td><input id="btn-" type="button" value="-" οnclick="showValue(this);"></td>
    </tr>
    <tr>
      <td><input id="btn7" type="button" value="7" οnclick="showValue(this);"></td>
      <td><input id="btn8" type="button" value="8" οnclick="showValue(this);"></td>
      <td><input id="btn9" type="button" value="9" οnclick="showValue(this);"></td>
      <td><input id="btn*" type="button" value="*" οnclick="showValue(this);"></td>
    </tr>
    <tr>
      <td><input id="btnClear" type="button" value="c" οnclick="valueClear();"></td>
      <td><input id="btn0" type="button" value="0" οnclick="showValue(this);"></td>
      <td><input id="btnResult" type="button" value="=" οnclick="valueResult();"></td>
      <td><input id="btn/" type="button" value="/" οnclick="showValue(this);"></td>
    </tr>
  </table>
  <script>
    function showValue(obj){
      document.getElementById("txt").value+=obj.value;
    }
    
    function valueResult(){
      var obj=document.getElementById("txt").value;
      if(obj.indexOf("=")<0){
        document.getElementById("txt").value+="="+eval(obj);
      }
    }
    
    function valueClear(){
      document.getElementById("txt").value="";
    }
  </script>
</body>
</html>



                
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值