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

javascript - Why does this ifelse statement always execute the if branch? - Stack Overflow

programmeradmin2浏览0评论

Hey guys I know that there exists .slideToggle() but I want to add more features later.

I don't know what I'm doing wrong, sliding up works but I cannot slide down.

Can I not overwrite my var? Would be very nice when somebody can help me.

$(document).ready(function () {

    var resizeValue = true;
    $(".resizeSelect").click(function () {
        if (resizeValue === true) {
            $(".resize").slideUp(

            function () {
                $('.parent').height($('.child').height('100'));
            });
            var resizeValue = false;
        } else {
            $(".resize").slideDown(

            function () {
                $('.parent').height($('.child').height('100'));
            });
            var resizeValue = true
        };
    });
});

Hey guys I know that there exists .slideToggle() but I want to add more features later.

I don't know what I'm doing wrong, sliding up works but I cannot slide down.

Can I not overwrite my var? Would be very nice when somebody can help me.

$(document).ready(function () {

    var resizeValue = true;
    $(".resizeSelect").click(function () {
        if (resizeValue === true) {
            $(".resize").slideUp(

            function () {
                $('.parent').height($('.child').height('100'));
            });
            var resizeValue = false;
        } else {
            $(".resize").slideDown(

            function () {
                $('.parent').height($('.child').height('100'));
            });
            var resizeValue = true
        };
    });
});
Share edited Feb 7, 2013 at 14:50 Felix Kling 818k181 gold badges1.1k silver badges1.2k bronze badges asked Feb 7, 2013 at 14:44 oliverwebroliverwebr 6274 silver badges16 bronze badges 1
  • 1 have you tried to put in some consol / alert logs, to see where the code hangs ? – Dukeatcoding Commented Feb 7, 2013 at 14:46
Add a ment  | 

2 Answers 2

Reset to default 8

You shouldn't redefine the resizeValue variable within the click function. Just remove var from var resizeValue (it should only be used at the top of the ready-function).

Because you are redeclaring your variable resizeValue in your function instead of update it :

$(document).ready(function () {

    var resizeValue = true;
    $(".resizeSelect").click(function () {
        if (resizeValue === true) {
            $(".resize").slideUp(

            function () {
                $('.parent').height($('.child').height('100'));
            });
            //DO NOT DECLARE NEW VARIABLE WITH VAR
            resizeValue = false;
        } else {
            $(".resize").slideDown(

            function () {
                $('.parent').height($('.child').height('100'));
            });
            //DO NOT DECLARE NEW VARIABLE WITH VAR
            resizeValue = true
        };
    });
});
发布评论

评论列表(0)

  1. 暂无评论