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>
JavaScript可以轻松地创建一个基本的计算器应用,通常涉及HTML用于构建用户界面,CSS用于样式,以及JavaScript处理用户的输入和计算。下面是一个简单的步骤说明: 1. **HTML结构**: 创建一个包含两个输入框(用于输入数字)、一个选择运算符的下拉菜单(如加、减、乘、除),以及两个按钮(一个用于触发计算,另一个清空输入)。 ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>简易计算器</title> </head> <body> <input type="number" id="num1" placeholder="第一个数"> <select id="operator"> <option value="+">+</option> <option value="-">-</option> <option value="*">*</option> <option value="/">/</option> </select> <input type="number" id="num2" placeholder="第二个数"> <button onclick="calculate()">计算</button> <button onclick="clearInputs()">清除</button> <p id="result"></p> </body> </html> ``` 2. **JavaScript代码**: 使用`querySelector`获取DOM元素,并添加事件监听器来处理计算和清除操作。 ```javascript function calculate() { const num1 = parseFloat(document.getElementById("num1").value); const num2 = parseFloat(document.getElementById("num2").value); const operator = document.getElementById("operator").value; let result; switch (operator) { case "+": result = num1 + num2; break; case "-": result = num1 - num2; break; case "*": result = num1 * num2; break; case "/": if (num2 !== 0) { result = num1 / num2; } else { alert("除数不能为零"); return; } break; default: // 如果选择了无效的操作符,不做任何操作 return; } document.getElementById("result").innerText = `结果: ${result}`; } function clearInputs() { document.getElementById("num1").value = ""; document.getElementById("num2").value = ""; document.getElementById("result").innerText = ""; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

饭九钦vlog

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

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

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

打赏作者

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

抵扣说明:

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

余额充值