布尔运算符未按预期工作

Mus*_*uaz -4 html javascript jquery

我有一个返回变量的函数为true.第一个变量是false,如果发生的事件变为true.通过这个真实状态,我想执行另一个函数.

我的Jquery代码是

var showContents = false,

if(showContents = true){
   $('.element').click(function(){
     close();
   })
}

var show = function(){
   return showContents = true        
}
Run Code Online (Sandbox Code Playgroud)

Glo*_*del 8

=不是'等于'运算符,使用=====代替.

随着showContents = true你赋值true给showContents.

  • 在适当的情况下将`=`更改为`==`之后,OP也可以简化`if(showContents == true)...`来简化`if(showContents)...` (3认同)