为什么这个if/else语句总是执行if分支?

qud*_*rat 0 javascript jquery

嘿家伙我知道存在,.slideToggle()但我想稍后添加更多功能.

我不知道我做错了什么,滑动工作,但我不能滑下来.

我可以不覆盖我的var吗?当有人可以帮助我时会很好.

$(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
        };
    });
});
Run Code Online (Sandbox Code Playgroud)

小智 8

您不应该resizeValueclick函数中重新定义变量.只是删除varvar resizeValue(它应该只在顶部使用ready-功能).


sde*_*ont 6

因为您在函数中重新声明变量resizeValue而不是更新它:

$(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
        };
    });
});
Run Code Online (Sandbox Code Playgroud)