简体   繁体   English

为什么我的javascript if else语句始终运行第一个选项?

[英]Why are my javascript if else statements always running the first option?

In my code I'm trying to set that if overflow is set to 'visible', it will remove the scrollbars using unloadScrollbars, and when overflow is set to 'hidden' it will not hide the scrollbars. 在我的代码中,我试图设置如果将溢出设置为“可见”,它将使用unloadScrollbars删除滚动条,而当将溢出设置为“隐藏”时,它将不会隐藏滚动条。 I'm using edge animate (which supports javascript and css), and every time I make an if statement it always executes the first option, whether or not the parameter is true. 我使用的是边缘动画(支持javascript和css),并且每次执行if语句时,无论参数是否为true,它始终执行第一个选项。 Please help! 请帮忙!

//overrides stage overflow setting
function overflow(toggle) {
    document.getElementById('Stage').style.overflow = toggle;
    return toggle;
}

overflow('hidden');


if (toggle='visible') {
    unloadScrollBars();
    console.log("unloading scrollbars");
} else {
    console.log("not unloading scrollbars");
}

= is an assignment =是一项作业 ...

if (toggle='visible') {

Is the same as: 是相同的:

toggle='visible';
if (toggle) {

… you want a comparison: == or === . …您想要比较: =====

if (toggle='visible') {

应该

if (toggle=='visible') {
toggle='visible'

This is assignment. 这是作业。 (toggle='vivible') is always true in javascript. (toggle='vivible')在javascript中始终为true

toggle == 'visible'

This performs a comparison, with type conversion if needed. 这将执行比较,并在需要时进行类型转换。

toggle === 'visible'

performs a comparison of variable type and variable content. 对变量类型和变量内容进行比较。

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

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