html+css+js的ios计算器

在这里插入图片描述

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="end_console.js"></script>
    <script src="jquery-1.12.2.js"></script>
    <style>
button,input,td,body{
    border: 0;
    margin: 0;
    padding: 0;
    outline:none; /* Remove the blue box that appears after clicking */
}

table{
    display: block;
    position:absolute;
    width: 400px;
    height: 620px;
    top:50%;
    left:50%;
    margin-top: -310px;
    margin-left: -200px;
    border-spacing: 0px;
    background-color: black;
}

td{
    width: 100px;
    height: 100px;
    text-align:center;
}
.output_txt {
    width: 340px;
    height: 100px;
    border:0px;
    color: white;
    font-size: 70px;
    text-align: right;
    margin-left: 0px;
    background-color: black;
}
.btn{
    width: 85px;
    height: 85px;
    border-radius: 50%;
    font-size:40px;
    color:#ffffff;
    background-color: #333333;
}

.btn_long{
    width: 180px;
    height: 80px;
    border-radius: 50px;
}

.orange_btn{
    background-color: #ff9705;
}

.gray_btn{
    width: 85px;
    height: 85px;
    border-radius: 50%;
    font-size:40px;
    color: black;
    background-color: #a6a6a6;
}
    </style>
</head>
<body>
    <table>
        <tr>
            <td colspan="4"><input class="output_txt" type="text"  value="0" disabled></td>
        </tr>
        <tr>
            <td><input class="gray_btn" type="button" value="AC"></td>
            <td><input class="gray_btn" type="button" value="+/-"></td>
            <td><input class="gray_btn" type="button" value="%"></td>
            <td><input class="btn orange_btn" type="button" value="÷"></td></tr>
        <tr>
            <td><input class="btn" type="button" value="7"></td>
            <td><input class="btn" type="button" value="8"></td>
            <td><input class="btn" type="button" value="9"></td>
            <td><input class="btn orange_btn" type="button" value="×"></td></tr>
        <tr>
            <td><input class="btn" type="button" value="4"></td>
            <td><input class="btn" type="button" value="5"></td>
            <td><input class="btn" type="button" value="6"></td>
            <td><input class="btn orange_btn" type="button" value="-"></td></tr>
        <tr>
            <td><input class="btn" type="button" value="1"></td>
            <td><input class="btn" type="button" value="2"></td>
            <td><input class="btn" type="button" value="3"></td>
            <td><input class="btn orange_btn" type="button" value="+"></td></tr>
        <tr>
            <td colspan="2"><input class="btn btn_long" type="button" value="0"></td>
            <td><input class="btn" type="button" value="."></td>
            <td><input class="btn orange_btn" type="button" value="="></td></tr>
    </table>
</body>
</html>

 function handle_undefined(text){
    var check = (parseInt(text)) * 0.01;
    text = toString(check)!== "object undefined"? check : "Error";
    return text;
}
function calculator(text){
    var output;
    try{
      output = eval(text);
    } catch(e){
        output= "Error";
    }
    return output;
}
window.onload = function () {
    let button = document.getElementsByClassName("btn");
    let expression = document.getElementsByClassName("output_txt")[0];
    let special_op = document.getElementsByClassName("gray_btn");
    let record = false;
    for (let i = 0; i < special_op.length; i++) {
        special_op[i].onclick = function () {
            if (this.value === "AC") expression.value = "0";
            else if (this.value === "+/-") expression.value = parseInt(expression.value)* (-1);
            else expression.value = handle_undefined(expression.value);
            }
    }
    for (let i = 0; i < button.length; i++) {
        button[i].onclick = function () {
            if (this.value === "."){
                if (expression.value.indexOf(".") === -1) expression.value += this.value;
            } else if (this.value === "÷" || this.value === "×"){
                if(this.value === "÷") expression.value += "/";
                else expression.value += "*";
            } else if(this.value === "="){
                expression.value = calculator(expression.value);
                record = true;
            } else {
                if(expression.value === "0" || record === true){
                    expression.value = this.value;
                    record = false;
                } else{
                    expression.value += this.value;
                }
            }
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值