用js做加减功能到1变灰

本文介绍了如何使用HTML、CSS和JavaScript创建一个简单的计数器,用户可以通过点击按钮增加或减少数字,当数字达到最大值时,停止加法操作。
摘要由CSDN通过智能技术生成

样式:

 <style>
      
        .sum{
            width: 300px;
            height: 200px;
            background-color: rgb(203,203,204);
            margin: 50px auto;
        }
        .sum_left{
            width: 200px;
            height: 190px;
            float: left;
            background-color: aliceblue;
            margin: 5px 5px;
            text-align: center;
            line-height: 190px;
            font-size: 50px;
        }
       button{
        width: 80px;
        height: 85px;
        font-size: 50px;
       }
      button:nth-of-type(1){
        margin-top: 5px;
      }
        
    </style>

html

<div class="sum">
        <div class="sum_left">1</div>
        <div class="sum_right">
           <p class="top"><button>+</button></p> 
            <p class="bottom"><button>-</button></p>
        </div>
    </div>

js

 <script>
        const sum_right=document.querySelector('.top>button')
        const sum_right1=document.querySelector('.bottom>button')
        const sum_left=document.querySelector('.sum_left')
        sum_right.addEventListener('click',function(){
             sum_left.innerHTML++
             if(sum_left.innerHTML==='1'){
                sum_right1.disabled=true
            }else{
                sum_right1.disabled=false
            }
        })
        sum_right1.addEventListener('click',function(){
               sum_left.innerHTML--
            if(sum_left.innerHTML==='1'){
                sum_right1.disabled=true
            }else{
                sum_right1.disabled=false
            }
        })

    </script>

效果

  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值