Web课外练习4

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>没啥实力的计算器设计</title>
    <style>
        .waiwei{
            border: 2px solid black;
            margin: auto;
        }
        .toubu {
            margin: auto; 
            
        }
        .xianshi{
            width: 210px;
        }
        #func{
            background-color:#E15572;
            border: 0cap;
            color: aliceblue;
            border-radius: 3px;
            width: 70px;
            height: 25px;
        }
        .kongge{
            width: 90px;
        }
        .kongge1{
            width: 25px;
        }
        .js{
            border-radius: 3px;
            width: 70px;
        }
    </style>
</head>
<body>
    <table class="waiwei">
        <th>
        <table class="toubu">
            <tr>
                <td class="title">
                    网页版计算器
                </td>
            </tr>
        </table>
        <table class="sub">
            <tr>
                <td colspan="2">
                    <input type="text" class="xianshi" placeholder="POWER OFF">
                </td>
                <td>
                    <input type="button" value="power" id="func" class="js">
                </td>
            </tr>
        </table>
        <table class="yunsuan">
            <tr></tr>
            <tr>
                <td class="kongge1"></td>
                <td class="f1">
                    <input type="button" value="clear" id="func" class="js">
                </td>
                <td class="kongge"></td>
                <td class="f2">
                    <input type="button" value="back" id="func" class="js">
                </td>
            </tr>
        </table>
        <table class="anniu">
            <tr></tr>
            <tr>
                <td>
                    <input type="button" value="1" class="js">
                </td>
                <td>
                    <input type="button" value="2" class="js">
                </td>
                <td>
                    <input type="button" value="3" class="js">
                </td>
                <td>
                    <input type="button" value="4" class="js">
                </td>
            </tr>
            <tr>
                <td>
                    <input type="button" value="5" class="js">
                </td>
                <td>
                    <input type="button" value="6" class="js">
                </td>
                <td>
                    <input type="button" value="7" class="js">
                </td>
                <td>
                    <input type="button" value="8" class="js">
                </td>
            </tr>
            <tr>
                <td>
                    <input type="button" value="9" class="js">
                </td>
                <td>
                    <input type="button" value="0" class="js">
                </td>
                <td>
                    <input type="button" value="." class="js">
                </td>
                <td>
                    <input type="button" value="=" class="js">
                </td>
            </tr>
            <tr>
                <td>
                    <input type="button" value="+" class="js">
                </td>
                <td>
                    <input type="button" value="-" class="js">
                </td>
                <td>
                    <input type="button" value="*" class="js">
                </td>
                <td>
                    <input type="button" value="/" class="js">
                </td>
            </tr>
        </table>
        </th>
    </table>
    <script>
        var display = document.querySelector('.xianshi');
        var operators = {
            '+': function(x, y) { return x + y; },
            '-': function(x, y) { return x - y; },
            '*': function(x, y) { return x * y; },
            '/': function(x, y) { return y ? x / y : 'Error'; }
        };
        var count=0;
        var flag=0;
        function countpower(){
            count+=1;
            if(count%2==0||count==0){
                display.value='';
                flag=0;
            }
            else{
                display.value='0';
            }
        }
        function updateDisplay(value) {
            display.value += value;
        }

        function clearDisplay() {
            display.value = '0';
        }

        function evaluate() {
            try {
                var result = eval(display.value);
                if (!isNaN(result)) {
                    display.value = result;
                } else {
                    display.value = 'Error';
                }
            } catch (e) {
                display.value = 'Error';
            }
        }
        function handleButtonClick(event) {
            var buttonValue = event.target.value;
            if (buttonValue=='power'){
                countpower();
            }
            if(count%2!=0){
            if (buttonValue === 'clear') {
                clearDisplay();
            } else if (buttonValue === 'back') {
                display.value = display.value.slice(0, -1);
            } else if (buttonValue === '=') {
                evaluate();
            } else{
                if(buttonValue!='power')
                {
                    flag+=1;
                    if(flag==1){
                    display.value='';}
                updateDisplay(buttonValue);
                }
            }
        }
        }

        var buttons = document.querySelectorAll('.js');
        for (var i = 0; i < buttons.length; i++) {
            buttons[i].addEventListener('click', handleButtonClick);
        }
    </script>
</body>
</html>

power按钮是计算器开关 

 效果图:

 

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值