js制作的简易计算器

<!DOCTYPE html>
<html lang="en" >
<head>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>计算器</title>
  <style>
  .all{
      margin:0px auto;
width:270px;
height:250px;
border:2px solid green;
background:yellow;
  }
.screen{
  width:258px;
  height:38px;
  text-align: right;
  padding-right:10px;
  font-size:20px;
}
.buttons{
width:50px;
height:50px;
border:1px solid black;
}
  </style>

</head>
<body οnlοad="onLoad()">
<div class="all">
    <!--screen输入栏-->
    <div class="screen">
      <input type="text" id="screenName" name="screenName" class="screen" value="" οnfοcus="jsq(this)">
    </div>
    <!-- operators and other keys -->
    <!--第一排-->
    <table>
    <tr><td><input type="button" id="7" οnclick="jsq(this.id)" value="7" class="buttons"></td>
    <td><input type="button" id="8" οnclick="jsq(this.id)" value="8" class="buttons"></td>
    <td><input type="button" id="9" οnclick="jsq(this.id)" value="9" class="buttons"></td>
    <td><input type="button" id="del" οnclick="tuiGe()" value="del" class="buttons"></td>
    <td><input type="button" id="C" οnclick="clearNum()" value="清零" class="buttons" style="margin-right:0px"></td></tr>
    <!--第二排-->
    <tr><td><input type="button" id="4" οnclick="jsq(this.id)" value="4" class="buttons"></td>
    <td><input type="button" id="5" οnclick="jsq(this.id)" value="5" class="buttons"></td>
    <td><input type="button" id="6" οnclick="jsq(this.id)" value="6" class="buttons"></td>
    <td><input type="button" id="*" οnclick="jsq(this.id)" value="*" class="buttons"></td>
    <td><input type="button" id="/" οnclick="jsq(this.id)" value="/" class="buttons" style="margin-right:0px"></td></tr>
    <!--第三排-->
    <tr><td><input type="button" id="1" οnclick="jsq(this.id)" value="1" class="buttons"></td>
    <td><input type="button" id="2" οnclick="jsq(this.id)" value="2" class="buttons"></td>
    <td><input type="button" id="3" οnclick="jsq(this.id)" value="3" class="buttons"></td>
    <td><input type="button" id="+" οnclick="jsq(this.id)" value="+" class="buttons"></td>
    <td><input type="button" id="-" οnclick="jsq(this.id)" value="-" class="buttons" style="margin-right:0px"></td></tr>
    <!--第四排-->
    <tr><td><input type="button" id="0" οnclick="jsq(this.id)" value="0" class="buttons"></td>
    <td><input type="button" id="." οnclick="jsq(this.id)" value="." class="buttons"></td>
    <td><input type="button" id="%" οnclick="jsq(this.id)" value="%" class="buttons"></td>
    <td><input type="button" id="eva" οnclick="eva()" value="=" class="buttons" style="margin-right:0px"></td>
 <td><input type="button" id="sqrt" οnclick="s()" value="根号" class="buttons"></td>
    </tr>
    </table>
    </div>
</body>
<script>
  var num = 0; // 定义第一个输入的数据
  function jsq(num) {
    //获取当前输入
    document.getElementById('screenName').value += document.getElementById(num).value;
  }
  function eva() {
    //计算输入结果
    document.getElementById("screenName").value = eval(document.getElementById("screenName").value);
  }
function s(){
var a=parseFloat(document.getElementById("screenName").value)
b=Math.sqrt(a);
 document.getElementById("screenName").value =b;
  document.getElementById("screenName").focus();
}
//开根号
  function clearNum() {
    //清0
    document.getElementById("screenName").value = null;
    document.getElementById("screenName").focus();
  }
  function tuiGe() {
    //退格
    var arr = document.getElementById("screenName");
    arr.value = arr.value.substring(0, arr.value.length - 1);
  }
  function onLoad(){
    //加载完毕后光标自动对应到输入框
    document.getElementById("screenName").focus();
  }

  /**
 *   ┏┓   ┏┓
 * ┏┛┻━━━------┛ ┻┓
 * ┃       ┃
 * ┃   ━  ┃
 * ┃ ┳┛ ┗┳ ┃
 * ┃      ┃
 * ┃   ┻  ┃
 * ┃      ┃
 * ┗━┓   ┏━┛
 *    ┃   ┃   
 *    ┃   ┃   
 *    ┃   ┗━━━┓
 *    ┃       ┣┓
 *    ┃     ┏┛
 *    ┗┓┓┏━       ┳┓┏┛
 *      ┃┫┫     ┃┫┫
 *      ┗┻┛     ┗┻┛
 *        神兽保佑
 *        代码无BUG!
 */
</script>

</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

饭九钦vlog

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值