javascript加减乘除简易计算器

初学js,跟着视频写了一个简易计算器,能实现简单的加减乘数,效果如图:
javascript加减乘除简易计算器_第1张图片
源代码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;
}

有兴趣的朋友也可以玩一玩^ _ ^

你可能感兴趣的:(web,css3,css,javascript)