gpt4 book ai didi

函数内的javascript函数不起作用?

转载 作者:行者123 更新时间:2023-12-02 18:15:19 24 4
gpt4 key购买 nike

编辑:新问题是即使 current_slide.showCurrentSlide();函数位于 hidePrevSlide 函数内部,showCurrentSLide 代码在 hidePrevSlide 中的动画完成之前执行。

我正在尝试创建一个网站,如果您单击

  • ,则
  • 将添加一个类。然后它将当前可见屏幕向上滑动并将其隐藏,然后显示与
  • 对应的屏幕(例如,如果
  • 是“home”,则它将当前现有屏幕向上滑动并将其隐藏,然后它应该是“#homeSlide”屏幕)。这是我的代码。

    $(document).ready(function(){

    hideItems(); //this function basically hides all the screens / slides.. The words 'screen' and 'slides' are interchangeable for the time being.

    $('#homeSlide').fadeIn(1000); //the default screen to be shown is the home screen
    $('#homeSlide').addClass('current'); //to signify that this is the current visible screen
    $('#home').addClass('clicked'); //#home is the <li>, so it adds the .clicked class to the <li>

    $('#sidebar ul a li').click(function(){ //loops through all <li>'s in the sidebar if an <li> is clicked
    var current_slide = $(this);
    $('#sidebar ul .clicked').removeClass('clicked'); // when an <li> is clicked, remove .clicked class from any other <li>'s
    current_slide.addClass('clicked'); // add .clicked class to the clicked <li> ($(this))

    hidePrevSlide(function(){
    alert('enter showing step');
    current_slide.showCurrentSlide();
    });
    });
    });

    这是我的 hidePrevSlide 函数。

    function hidePrevSlide(){
    var test = $('.current').attr('id');
    test = "#" + test; // surrounds the id with a # and the word 'Slide'. This is the id of the screen which should slideUp
    $(test).slideUp( function () {
    $(test).hide();
    $(test).removeClass('current');
    });
    alert('finished hiding step. Should enter showing step now');
    };

    现在,当我运行代码时,它确实显示“已完成隐藏步骤”。现在应该进入显示步骤”,但它没有说“输入显示步骤”,因此它不会进入 hidePrevSlide 函数完成后应执行的步骤。怎么会这样?

  • 最佳答案

    hidePrevSlide需要调用回调函数。

    function hidePrevSlide(callback){
    var test = $('.current');
    test.slideUp( function () {
    test.hide();
    test.removeClass('current');
    });
    alert('finished hiding step. Should enter showing step now');
    callback();
    };

    我还删除了 $(test) 的使用。如果您有一个元素,则无需继续通过 ID 搜索它。

    关于函数内的javascript函数不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19350288/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com