javascript - 如何更改JS中的 boolean 值?

标签 javascript html css dom boolean

我正在尝试制作一个具有两个功能的按钮:

function bigfont()
{var font_is_small = true
if (font_is_small = true)
{document.getElementById('one').className=
document.getElementById('one').className.replace("font1","font2");
document.getElementById('two').className=
document.getElementById('two').className.replace("font1","font2");
document.getElementById('three').className=
document.getElementById('three').className.replace("font1","font2");
document.getElementById('four').className=
document.getElementById('four').className.replace("font3","font4"); 
font_is_small = true;}
if(font_is_small = false)
{document.getElementById('one').className=
document.getElementById('one').className.replace("font2","font1");
document.getElementById('two').className=
document.getElementById('two').className.replace("font2","font1");
document.getElementById('three').className=
document.getElementById('three').className.replace("font2","font1");
document.getElementById('four').className=
document.getElementById('four').className.replace("font4","font3");
font_is_small = true;}}    

但是变量并没有改变。谁能帮帮我?

最佳答案

要将 boolean 值更改为其相反的值,您可以使用否定 (!),例如 x = !x 表示“设置 x如果为真则为 false,如果为假则为 true
如果您希望函数在小字体和大字体之间切换,最简单的方法是将变量放在函数之外:
http://jsfiddle.net/zvoeLu9p/

var font_is_small = true;
function bigfont()
{
    font_is_small = !font_is_small; // switch the boolean
    if (font_is_small){ // no need for == true
        document.body.className=
        document.body.className.replace("font1","font2");
    }
     else { // no need for if condition
        document.body.className=
        document.body.className.replace("font2","font1");
    }
 }    

关于javascript - 如何更改JS中的 boolean 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27335199/

相关文章:

c# - 从字符串中删除 HTML

php - 无法更改 CSS 字体类

javascript - 将 DIV 移出屏幕而不影响宽度?

html - HTML <table> 标签用于导航栏是否不合适或不正确?

javascript - 使用引导下拉子菜单

javascript - 在 UIWebView 中禁用 Javascript

html - 在 Jekyll 中设置事件链接不起作用

html - Bootstrap 3.1.1 导航栏表单反馈未对齐

javascript - 超过 24 小时的时间字段(计时器)

javascript - jquery p :last-child not working