开发者

Javascript If Else nesting

开发者 https://www.devze.com 2023-01-20 02:17 出处:网络
Hello here is my code : var one = 0; var two = 0; var three = 0; var free = 1; function open() { if ($(\'#one\').is(\':visible\')) {

Hello here is my code : var one = 0; var two = 0; var three = 0; var free = 1; function open() {

    if ($('#one').is(':visible')) {
        one = 1;
    }
    else {
        one = 0;
        free = 1;
    }

    if ($('#two').is(':visible')) {
        two = 1;
    }
    else {
        two = 0;
        if (one == 1) {
            free = 2;
        }
    }

    if ($('#three').is(':visible')) {
        three = 1;
    }

    else {
        three = 0;
        if (one == 1 && two == 1) {
            free = 3;
        }

    }
}

and then in the HTML part

<body onload="javascript:window.setInterval('open()', 1000)">

Now the layers one two and three are hidden by default. Now the problem is, at first the HTML output ( in Layer 7) is 1 (value of free) . But after one second it changes to 2. Shouldn't it remain the same? This i开发者_如何学运维s as all the layers have the same visibility(hidden) at each point in time...


give a class ('layers' for example) to each layer in order to collect them all together. Then loop them to find the first non visible and get the id.

var free = undefined;

$('.layers').each(function()
{
    if(!$(this).is(':visible'))
    {
        free = $(this).attr('id');
        break;
    }
});

if(free != undefined)
    alert('layer '+free+' is free');
else
    alert('there are no free layers');
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号