简体   繁体   English

jQuery click事件首次触发但第二次失败。页面刷新时有效

[英]jquery click event fires for the first time but fails second time.It works when page refreshes

Im opening a div on click of another div.Below code seems to be working only for the first time. 我在点击另一个div时打开了一个div,下面的代码似乎只在第一次起作用。
It works when page refreshes. 页面刷新时有效。 Close event always get called but click event does not fire. 关闭事件总是被调用,但是点击事件不会触发。

$('.redirection-list .redirection a').on('click', clickevent);


function clickevent() {
    $(this).attr("href", "#myCurrentDiv");
    $('.redirection-list li').removeAttr('id');
    $(this).closest('li').attr("id", "myCurrentDiv");
    $('.redirection-list .redirection').removeClass('active');
    $(this).closest('li').addClass('active');
    $('.block-list').removeClass('col-sm-12');
    $('.block-list').addClass('col-sm-6');
    $('.redirection-edit').addClass('open');

    $('.redirection-status span:nth-of-type(2n)').hide();
    $('.redirection-status span:nth-of-type(n)').css('border-radius', '50px');

    if ($(window).width() <= 768) {
        $('body').addClass('mobile-device');
    }
    else {
        $('body').removeClass('mobile-device');
    }

    $('.redirection-edit').insertAfter(this);
    $('.redirection-edit').fadeIn(700);
    if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') && location.hostname == this.hostname) {
        var target = $(this.hash);
        target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
        if (target.length) {
            if ($(target).closest('.block-list').hasClass('search-list')) {
                $('html, body').animate({
                    scrollTop: target.offset().top - 185
                }, 500);
                return false;
            }
            else {
                $('html, body').animate({
                    scrollTop: target.offset().top - 112
                }, 500);
                return false;
            }
        }
    }
}


$('.redirection-edit .close').click(function () {
    $('.redirection-edit').removeClass('open');
    $('.redirection-edit').fadeOut(500);
    $('.redirection-list .redirection').removeClass('active');
    $('.block-list').removeClass('col-sm-6');
    $('.block-list').addClass('col-sm-12');

    $('.redirection-status span:nth-of-type(2n)').show();
    $('.redirection-status span:nth-of-type(n)').css('border-radius', '50px 0px 0px 50px');
    $('.redirection-status span:nth-of-type(2n)').css('border-radius', '0px 50px 50px 0px');
    $('.redirection-list .redirection a').bind('click',clickevent)
});

First of all too much codes and lack of HTML code makes the question confusing. 首先,太多的代码和HTML代码的缺乏使这个问题变得令人困惑。 anyway, as far as I understand the event handler is not getting bind to the anchor once done already, which may occur if the anchor is being manipulated or created dynamically if that is the case changing the code as below will do the trick 无论如何,据我所知,事件处理程序一旦完成就不会绑定到锚,如果正在动态地操作或创建锚,则可能会发生这种情况(如下所示)

$('body').on('click', '.redirection-list .redirection a', clickevent);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM