js简易计算器

初学js,跟着视频做了一个简单的计算器,基本的加减乘除没有问题

源代码如下,HTML部分:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>js计算器</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <form class="calculator" name="calc">
        <input class="value" type="text" name="txt" readonly="">
        <span class="num clear" onclick="document.calc.txt.value = ''">C</span>
        <span class="num" onclick="document.calc.txt.value += '/'">/</span>
        <span class="num" onclick="document.calc.txt.value += '*'">*</span>
        <span class="num" onclick="document.calc.txt.value += '7'">7</span>
        <span class="num" onclick="document.calc.txt.value += '8'">8</span>
        <span class="num" onclick="document.calc.txt.value += '9'">9</span>
        <span class="num" onclick="document.calc.txt.value += '-'">-</span>
        <span class="num" onclick="document.calc.txt.value += '4'">4</span>
        <span class="num" onclick="document.calc.txt.value += '5'">5</span>
        <span class="num" onclick="document.calc.txt.value += '6'">6</span>
        <span class="num plus" onclick="document.calc.txt.value += '+'">+</span>
        <span class="num" onclick="document.calc.txt.value += '3'">3</span>
        <span class="num" onclick="document.calc.txt.value += '2'">2</span>
        <span class="num" onclick="document.calc.txt.value += '1'">1</span>
        <span class="num" onclick="document.calc.txt.value += '0'">0</span>
        <span class="num" onclick="document.calc.txt.value += '00'">00</span>
        <span class="num" onclick="document.calc.txt.value += '.'">.</span>
        <span class="num equal" onclick="document.calc.txt.value = eval(calc.txt.value )">=</span>
    </form>
</body>
</html>

css部分:

*{
    padding: 0%;
    margin: 0%;
    font-family: Arial, Helvetica, sans-serif;
    box-sizing: border-box;
}

body{
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    background: #091921;
}

.calculator{
    position: relative;
    display: grid;
}

.calculator .value{
    grid-column: span 4;
    height: 100px;
    text-align: right;
    border: none;
    outline: none;
    padding: 10px;
    font-size: 18px;
    background: whitesmoke;
}

.calculator span{
    display: grid;
    width: 60px;
    height: 60px;
    color: #fff;
    background: #0c2835;
    place-items: center;
    border: 1px solid rgba(0,0,0,0.1);
}

.calculator span:active{
    background: #74ff3b;
    color: #111;
}

.calculator span.clear{
    grid-column: span 2;
    width: 120px;
    background: salmon;
}

.calculator span.plus{
    grid-row: span 2;
    height: 120px;
}

.calculator span.equal{
    background: skyblue;
}

有兴趣的可以自己去玩一玩^_^

点赞

发表评论

昵称和uid可以选填一个,填邮箱必填(留言回复后将会发邮件给你)
tips:输入uid可以快速获得你的昵称和头像

Title - Artist
0:00