我点击另一个div打开一个div.Below代码似乎只是第一次工作。
页面刷新时有效。始终会调用关闭事件,但不会触发click事件。
$('.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)
});
答案 0 :(得分:0)
首先,太多的代码和缺乏HTML代码使问题变得混乱。无论如何,据我所知,事件处理程序一旦完成就没有绑定到锚点,如果正在动态操作或动态创建锚点,可能会发生这种情况,如果是这样的情况,则更改代码将如下所示
$('body').on('click', '.redirection-list .redirection a', clickevent);