Prevent click event in jQuery triggering multiple times

后端 未结 6 603
甜味超标
甜味超标 2020-12-10 15:01

I have created a jQuery content switcher. Generally, it works fine, but there is one problem with it. If you click the links on the side multiple times, multiple pieces of c

6条回答
  •  遥遥无期
    2020-12-10 15:43

    I toyed around with the code earlier and came up with the following modification which seems to work:

    $('#tab-list li a').click(
        function() {
            $('.tab:animated').stop(true, true);
            var targetTab = $(this).attr('href');
            if ($(targetTab).is(':hidden')) {
                $('#tab-list li').removeClass('selected');
                var targetTabLink = $(this).parents('li').eq(0);
                $(targetTabLink).addClass('selected');
                $('.tab:visible').fadeOut('slow',
                    function() {
                        $(targetTab).fadeIn('slow');
                    }
                );
            }
            return false;
        }
    );
    

    All that happens is, when a new tab is clicked, it immediately brings the current animation to the end and then begins the new transition.

提交回复
热议问题