最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

css - Why are my javascript if else statements always running the first option? - Stack Overflow

programmeradmin7浏览0评论

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");
}

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");
}
Share Improve this question asked Mar 21, 2014 at 16:19 teo751teo751 3211 gold badge4 silver badges13 bronze badges 2
  • Any working fiddles to show? – Muhammed Basil Commented Mar 21, 2014 at 16:20
  • 2 use toggle == 'visible'. but toggle is never set. – user3401335 Commented Mar 21, 2014 at 16:21
Add a ment  | 

3 Answers 3

Reset to default 8

= is an assignment

if (toggle='visible') {

Is the same as:

toggle='visible';
if (toggle) {

… you want a parison: == or ===.

if (toggle='visible') {

should be

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

This is assignment. (toggle='vivible') is always true in javascript.

toggle == 'visible'

This performs a parison, with type conversion if needed.

toggle === 'visible'

performs a parison of variable type and variable content.

发布评论

评论列表(0)

  1. 暂无评论