jQuery 如何将变量从真切换为假

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5477627/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 19:14:27  来源:igfitidea点击:

How to toggle a variable from true to false

jquerybuttontoggle

提问by Patrioticcow

I have this setup: http://jsfiddle.net/patrioticcow/yJPGa/5/

我有这个设置:http: //jsfiddle.net/patrioticcow/yJPGa/5/

I can't figure out how how to toggle in between a true or false variable. Here is the code:

我不知道如何在 true 或 false 变量之间切换。这是代码:

<div class="test" id="id_test">Some Content...</div>
<div style="display: none" id="id_test">Some Other Content...</div>
<div>
  <button id="disable">Save</button>
  <button id="enable">Edit</button>
</div>

js

js

var logged_in = false;

$("#disable").click(function() {
  logged_in == true;
  alert (logged_in);
});

$("#enable").click(function() {
  logged_in == false;
  alert (logged_in);
});

if (logged_in == true) {
  $("#id_test").find(".test").removeClass(".test").addClass(".test_hidde");
}

css

css

.test{color: red;font-size: 16px;}
.test_hidde{color: #000;font-size: 26px;}

采纳答案by Naftali aka Neal

Here ya go in a fiddle: http://jsfiddle.net/maniator/yJPGa/7/

在这里你在小提琴:http: //jsfiddle.net/maniator/yJPGa/7/

changed the lines: logged_in == true;==> logged_in = true;
and logged_in == true;==> logged_in = false;

更改了行:logged_in == true;==>logged_in = true;
logged_in == true;==>logged_in = false;

==denotes boolean check whereas =sets something as equal to something else

==表示布尔检查,而=将某些内容设置为等于其他内容



Here is a fiddle with the if statement fixed: http://jsfiddle.net/maniator/yJPGa/8/

这是修复了 if 语句的小提琴:http: //jsfiddle.net/maniator/yJPGa/8/

The statement has to be encapsulated in a function

语句必须封装在函数中

回答by Dean Barnes

logged_in = !logged_in

Will do the trick.

会做的伎俩。

Also, these two lines are the same:

此外,这两行是相同的:

if (logged_in == true)
if (logged_in)