簡體   English   中英

單擊后打開和關閉jQuery功能

[英]jquery on and off function after click

我在我的網站上使用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”,將不會執行bottom_open()。 /

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

/ 然后進行第二操作,當單擊“ .bottom_close”時,將顯示第二個函數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”時,將執行第二個函數bottom_close()並重新綁定第一個函數bottom_open(),因此,如果再次單擊“ .cartouche_bottom_inside”,將執行bottom_open()等等。 。

我找不到我在做什么錯。

有人可以幫我嗎?

非常感謝您的時間和幫助,

您可以將函數綁定到某些類,然后添加這些類並根據需要將它們刪除到適當位置的元素中。

編輯:另外,看起來您在這里有錯字:

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

應該是(請注意選擇器上缺少的“。”):

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

我只想添加和刪除類似的類:

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

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

})

嘗試這個

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM