jQuery计算器

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>jQuery计算器</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            background-color: #f5f5f5;
            margin: 0;
        }
        .calculator {
            background-color: #fff;
            border-radius: 10px;
            box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
            padding: 20px;
            width: 300px;
        }
        .display {
            background-color: #f9f9f9;
            border: 1px solid #ddd;
            border-radius: 5px;
            padding: 15px;
            margin-bottom: 15px;
            text-align: right;
            font-size: 24px;
            height: 30px;
        }
        .buttons {
            display: grid;
            grid-template-columns: repeat(4, 1fr);
            grid-gap: 10px;
        }
        button {
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 5px;
            padding: 15px;
            font-size: 18px;
            cursor: pointer;
            transition: background-color 0.3s;
        }
        button:hover {
            background-color: #45a049;
        }
        .operator {
            background-color: #ff9800;
        }
        .operator:hover {
            background-color: #e68a00;
        }
        .clear {
            background-color: #f44336;
        }
        .clear:hover {
            background-color: #d32f2f;
        }
        .equals {
            background-color: #2196F3;
        }
        .equals:hover {
            background-color: #0b7dda;
        }
        .footer {
            margin-top: 20px;
            text-align: center;
            font-size: 12px;
            color: #666;
        }
    </style>
</head>
<body>
    <div class="calculator">
        <div class="display" id="display">0</div>
        <div class="buttons">
            <button class="clear" id="clear">C</button>
            <button class="operator" id="backspace">←</button>
            <button class="operator" id="percent">%</button>
            <button class="operator" id="divide">/</button>
            
            <button class="number" id="seven">7</button>
            <button class="number" id="eight">8</button>
            <button class="number" id="nine">9</button>
            <button class="operator" id="multiply">×</button>
            
            <button class="number" id="four">4</button>
            <button class="number" id="five">5</button>
            <button class="number" id="six">6</button>
            <button class="operator" id="subtract">-</button>
            
            <button class="number" id="one">1</button>
            <button class="number" id="two">2</button>
            <button class="number" id="three">3</button>
            <button class="operator" id="add">+</button>
            
            <button class="number" id="zero">0</button>
            <button class="number" id="decimal">.</button>
            <button class="equals" id="equals" colspan="2">=</button>
        </div>
        <div class="footer">
            <p>简单计算器 - 由jQuery驱动</p>
        </div>
    </div>

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function() {
            let currentInput = '0';
            let previousInput = '';
            let operation = null;
            let resetScreen = false;
            
            // 更新显示
            function updateDisplay() {
                $('#display').text(currentInput);
            }
            
            // 数字按钮点击事件
            $('.number').click(function() {
                const number = $(this).text();
                
                if (currentInput === '0' || resetScreen) {
                    currentInput = number;
                    resetScreen = false;
                } else {
                    currentInput += number;
                }
                
                updateDisplay();
            });
            
            // 运算符按钮点击事件
            $('.operator').click(function() {
                const operator = $(this).text();
                
                if ($(this).attr('id') === 'backspace') {
                    // 退格功能
                    if (currentInput.length === 1) {
                        currentInput = '0';
                    } else {
                        currentInput = currentInput.slice(0, -1);
                    }
                    updateDisplay();
                    return;
                }
                
                if ($(this).attr('id') === 'percent') {
                    // 百分比功能
                    currentInput = (parseFloat(currentInput) / 100).toString();
                    updateDisplay();
                    return;
                }
                
                if (operation !== null) calculate();
                previousInput = currentInput;
                operation = operator;
                resetScreen = true;
            });
            
            // 等号按钮点击事件
            $('#equals').click(function() {
                calculate();
                operation = null;
            });
            
            // 清除按钮点击事件
            $('#clear').click(function() {
                currentInput = '0';
                previousInput = '';
                operation = null;
                updateDisplay();
            });
            
            // 小数点按钮点击事件
            $('#decimal').click(function() {
                if (resetScreen) {
                    currentInput = '0.';
                    resetScreen = false;
                    updateDisplay();
                    return;
                }
                
                if (!currentInput.includes('.')) {
                    currentInput += '.';
                    updateDisplay();
                }
            });
            
            // 计算函数
            function calculate() {
                let result;
                const prev = parseFloat(previousInput);
                const current = parseFloat(currentInput);
                
                if (isNaN(prev) || isNaN(current)) return;
                
                switch (operation) {
                    case '+':
                        result = prev + current;
                        break;
                    case '-':
                        result = prev - current;
                        break;
                    case '×':
                        result = prev * current;
                        break;
                    case '/':
                        result = prev / current;
                        break;
                    default:
                        return;
                }
                
                currentInput = result.toString();
                resetScreen = true;
                updateDisplay();564
            }
            
            // 初始化显示
            updateDisplay();
        });
    </script>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值