html计算器

以下是一个更复杂的计算器的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()">Cdiv>
            <div class="button" onclick="appendValue('7')">7div>
            <div class="button" onclick="appendValue('8')">8div>
            <div class="button" onclick="appendValue('9')">9div>
            <div class="button" onclick="appendValue('4')">4div>
            <div class="button" onclick="appendValue('5')">5div>
            <div class="button" onclick="appendValue('6')">6div>
            <div class="button" onclick="appendValue('1')">1div>
            <div class="button" onclick="appendValue('2')">2div>
            <div class="button" onclick="appendValue('3')">3div>
            <div class="button" onclick="appendValue('0')">0div>
            <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>

你可能感兴趣的:(javascript)