简体   繁体   English

多个数字相加计数 jquery

[英]add multiple numbers to count jquery

this is the dead script for my shooter game (the buttons are just for testing) but for some reason i cant figure out how to add 10 instead of 1这是我的射击游戏的死脚本(按钮仅用于测试)但由于某种原因我无法弄清楚如何添加 10 而不是 1

this is what i have now:这就是我现在所拥有的:

(function(){
    const buttons = document.querySelectorAll('.counterBtn')
    let count= 0
    
    //Add event listeners and functionailty to each button  
    buttons.forEach(function(button){
      button.addEventListener('click', function(){

        //buttons
        if (button.classList.contains('-1')){
          count--
        } else if (button.classList.contains('+1')){
          count++
        } else if (button.classList.contains('+10')){
          
        }
  
        //Select the counter text
        const counter = document.querySelector('#counter')
        counter.textContent = count
  
        //dead / alive
        if (count <= 0 ){            
            counter.style.color = 'red'
            console.log("dead")
        } else if (count > 0){
            counter.style.color = 'lightgreen'
            console.log("alive")
        } else {
          counter.style.color = '#000000'
        }

        if (count <= 0) {
          alert ("you died")
        } else if (count > 100 ) {
          alert ("u r ful health")
        } else {
          return
        }
      })
    })
  })()

can someone please help me?有人可以帮帮我吗?

Use += , for example:使用+= ,例如:

count += 10

Of course this also works, but is more verbose:当然这也有效,但更冗长:

count = count + 10

See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators#Assignment请参阅https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators#Assignment

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM