简体   繁体   中英

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. 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. 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 == 'visible'

This performs a comparison, with type conversion if needed.

toggle === 'visible'

performs a comparison of variable type and variable content.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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