html计算器

<!DOCTYPE html>
<html>
<head>
    <title>计算器</title>
    <style>
        .container {
            width: 300px;
            margin: 0 auto;
            padding: 20px;
            border: 1px solid #ccc;
            border-radius: 5px;
        }
        .display {
            width: 100%;
            height: 60px;
            margin-bottom: 10px;
            padding: 10px;
            font-size: 30px;
            text-align: right;
            background-color: #f1f1f1;
            border: none;
            border-radius: 5px;
        }
        .buttons {
            display: grid;
            grid-template-columns: repeat(4, 1fr);
            grid-gap: 10px;
        }
        .button {
            width: 70px;
            height: 50px;
            background-color: #eee;
            border-radius: 5px;
            font-size: 20px;
            text-align: center;
            line-height: 50px;
            cursor: pointer;
        }
        .button:hover {
            background-color: #ddd;
        }
        .button.clear {
            grid-column: span 2;
        }
        .button.equals {
            background-color: #f39c12;
            color: #fff;
        }
    </style>
</head>
<body>
    <div class="container">
        <input type="text" id="display" class="display" readonly>
        <div class="buttons">
            <div class="button clear" onclick="clearDisplay()">C</div>
            <div class="button" onclick="appendValue('7')">7</div>
            <div class="button" onclick="appendValue('8')">8</div>
            <div class="button" onclick="appendValue('9')">9</div>
            <div class="button" onclick="appendValue('4')">4</div>
            <div class="button" onclick="appendValue('5')">5</div>
            <div class="button" onclick="appendValue('6')">6</div>
            <div class="button" onclick="appendValue('1')">1</div>
            <div class="button" onclick="appendValue('2')">2</div>
            <div class="button" onclick="appendValue('3')">3</div>
            <div class="button" onclick="appendValue('0')">0</div>
            <div class="button" onclick="appendValue('.')">.</div>
            <div class="button" onclick="appendValue('+')">+</div>
            <div class="button" onclick="appendValue('-')">-</div>
            <div class="button" onclick="appendValue('*')">*</div>
            <div class="button" onclick="appendValue('/')">/</div>
            <div class="button" onclick="calculate()">=</div>
        </div>
    </div>

    <script>
        function appendValue(value) {
            var display = document.getElementById('display');
            var lastChar = display.value.slice(-1);
            if (lastChar === '.' && value === '.') {
                return;
            }
            display.value += value;
        }

        function calculate() {
            var display = document.getElementById('display');
            var expression = display.value;
            if (expression) {
                try {
                    var result = eval(expression);
                    display.value = result;
                } catch (error) {
                    display.value = '错误';
                }
            }
        }

        function clearDisplay() {
            document.getElementById('display').value = '';
        }
    </script>
</body>
</html>

你可能感兴趣的:(1024程序员节)