单击后jquery打开和关闭功能

时间:2014-12-12 17:29:23

标签: javascript jquery function bind unbind

我在我的网站上使用了2个jquery代码。

第一个代码是:

    function  bottom_open() {


        $('.cartouche_bottom').css('position','absolute').animate({
        top :$(".top_table").height() - 1,
        });

        $('.bottom_close').show();
        //$('.layout_bottom').stop().animate({height :0});

    }

,第二个是:

    function  bottom_close() {



        $('.cartouche_bottom').css('position','fixed').animate({
        top : cartouche_bottom_position
    });
        $('.bottom_close').hide();
}

}

我想要做的是当第一个函数被执行时(通过单击函数),取消绑定该函数,然后当第二个函数被执行时,重新绑定第一个函数。我无法找到办法。

这是我尝试过的:

function bottom_cartouche(){

    var cartouche_bottom_position = $(window).height() - $('.top_table').height() - $('.layout_bottom').height();

/ 第一个行动,当" .cartouche_bottom_inside"单击,执行函数bottom_open(),然后取消绑定bottom_open()。 因此,如果我们再次点击" .cartouche_bottom_inside" base_open()将不会被执行。 /

    $('.cartouche_bottom_inside').click(function(){
    bottom_open();
    $(this).off('click');
    });

/ 然后是第二次行动,当" .bottom_close"单击exectude第二个函数bottom_close()并重新绑定第一个函数bottom_open()。 因此,如果我们再次点击" .cartouche_bottom_inside",将执行bottom_open() /

    $('.bottom_close').click(function(){
    bottom_close();
    $('cartouche_bottom_inside').on('click');

    });

        function  bottom_open() {


            $('.cartouche_bottom').css('position','absolute').animate({
            top :$(".top_table").height() - 1,
            });

            $('.bottom_close').show();
            //$('.layout_bottom').stop().animate({height :0});

        }

        function  bottom_close() {



            $('.cartouche_bottom').css('position','fixed').animate({
            top : cartouche_bottom_position
        });
            $('.bottom_close').hide();
    }
}

这是一个解释:

第一步,当" .cartouche_bottom_inside"单击,执行函数bottom_open(),然后取消绑定bottom_open()。 因此,如果我们再次点击" .cartouche_bottom_inside"不会执行bottom_open()。

然后是第二次行动,当" .bottom_close"单击exectude第二个函数bottom_close()并重新绑定第一个函数bottom_open() 因此,如果我们再次点击" .cartouche_bottom_inside",将执行bottom_open()......依此类推......

我无法找出我做错了什么。

有人可以帮我吗?

非常感谢您的时间和帮助,

3 个答案:

答案 0 :(得分:0)

您可以将函数绑定到某些类,然后添加类并根据需要将它们移除到适当位置的元素中。

编辑:此外,看起来你有一个拼写错误:

$('.bottom_close').click(function(){
    bottom_close();
    $('cartouche_bottom_inside').on('click');
});

应该是(注意选择器上缺少的'。':

$('.bottom_close').click(function(){
    bottom_close();
    $('.cartouche_bottom_inside').on('click');
});

答案 1 :(得分:0)

我只想添加和删除类似于:

的类
    $('#buttonhere').click(function (e) {

    if ($(this).hasClass('firstPart')) {
        // Do one part.
        bottom_open();
        $(this).toggleClass('firstPart');
    }
    else {
        bottom_close();
        $(this).toggleClass('firstPart');
    }

})

答案 2 :(得分:0)

试试这个

$('.bottom_close').click(function(){
  bottom_close();
  $('.cartouche_bottom_inside').on('click', function() {bottom_open() });
});

相关问题