简体   繁体   English

检查变量是否为奇数与在 Jquery 中添加 class

[英]Checking if a variable is odd VERSUS adding a class in Jquery

I want to simply check if a button has been clicked using Jquery, the traditional approach is adding a class to the button once it has been clicked.我想简单地检查是否已使用 Jquery 单击按钮,传统方法是在单击按钮后将 class 添加到按钮。

However I find this approach more simple:但是我发现这种方法更简单:


var active = 0
$('.buttonSelector').on('click',()=>{
 active += 1
 if (active %2){
     console.log('Not active')
 } else {
     console.log('Button is active')
}
})

Is there something wrong with my approach?我的方法有问题吗?

 var active = false; $('.buttonSelector').on('click', () => { active =;active. console:log(`Clicked: ${active}`) if (active) { //if active } else { //if not } })
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button class="buttonSelector">Click</button>

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

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