クリック時にdivを表示/非表示にしますが、同じリンクが2回クリックされた場合はスクリプトが実行されないようにします

jpscoggan

いずれかのリンクをクリックすると、スクリプトは正常に機能します。同じリンクが2回クリックされた場合に、表示/非表示スクリプトが実行されないようにする必要があります。このサイトや他のサイトで、.unbindが最も有望であると試すことがいくつか見つかりましたが、私の関数はで始まり、バインドを解除$(this).click(function ()するとすべてが強制終了されます。コードを再構築する方法がわからないためtoggleDiv、クリック前とクリックtoggleDivを比較して、コードが同じであるかどうかを確認し、スクリプトを削除します。

Codepen:http://codepen.io/jpscoggan/pen/gcHbK

これが私のjsコードです:

//show hide
(function ($) {

    $.fn.showHide = function (options) {

        //default vars for the plugin
        var defaults = {
            speed: 200,
            easing: 'swing'
        };
        var options = $.extend(defaults, options);

        $(this).click(function () {
            $('.toggleDiv').slideUp(options.speed, options.easing);
            // once the button is clicked, the clicked div (.toggleDiv) slides up  
            var toggleDiv = $(this).attr('rel');
            // reads rel attribute of clicked link / which div id to toggle 
            $(toggleDiv).slideToggle(options.speed, options.easing);
            // toggle show/hide the correct div + speed and easing

            e.preventDefault();
        });
    };
})(jQuery);

$('.show_hide').showHide({
    speed: 200,
    // speed you want the toggle to happen             
});
musicnothing

これを行う簡単な方法:クリックしたクラスにクラスを追加し、そのリンクがアクティブな場合は2回目のクリックを禁止します。

Codepen

$('.show_hide section').click(function() {
    if ($(this).hasClass('active')) return false;
    //use if you want to allow a click after switching sections
    $('.active').removeClass('active');
    $(this).addClass('active');
});

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

Related 関連記事

ホットタグ

アーカイブ