Jquery 添加单击返回到未绑定事件
当我运行下面的代码时,我只希望#overlay、#image+the number和#close处于活动状态,因此我禁用了$('.box')。单击#close后,我想重新启用单击$('.box'),但到目前为止我无法启用。我检查了其他答案,但没能把它们放在一起。谢谢你的帮助 var handler = function() { var theIDval = $(this).attr('id'); var theNumber = theIDval.replace('box',''); $('.box').unbind('click'); $('#overlay').show(); $('#image' + theNumber).fadeIn(); $('#close').fadeIn(); $('#close').click( function () { $('#overlay').fadeOut(); $('#image' + theNumber).fadeOut(); $('#close').fadeOut(); }); }; $(document).ready( function() { $('.box').click(handler); }); var handler=function(){ var theIDval=$(this.attr('id'); var THENNUMBER=数字替换('框',''); $('.box')。解除绑定('click'); $(“#覆盖”).show(); $(“#image”+数字).fadeIn(); $('#close').fadeIn(); $(“#关闭”)。单击(函数(){ $(“#覆盖”).fadeOut(); $(“#图像”+数字).fadeOut(); $(“#关闭”).fadeOut(); }); }; $(文档).ready(函数(){ $('.box')。单击(处理程序); });Jquery 添加单击返回到未绑定事件,jquery,click,bind,unbind,Jquery,Click,Bind,Unbind,当我运行下面的代码时,我只希望#overlay、#image+the number和#close处于活动状态,因此我禁用了$('.box')。单击#close后,我想重新启用单击$('.box'),但到目前为止我无法启用。我检查了其他答案,但没能把它们放在一起。谢谢你的帮助 var handler = function() { var theIDval = $(this).attr('id'); var theNumber = theIDval.replace('box','');
如果我是你,我会这样做:
var handler = function() {
var $box = $('.box');
if($box.hasClass('disabled')){
// do nothing, just disallow further spawns
} else{
$box.addClass('disabled');
var theIDval = $(this).attr('id');
var theNumber = theIDval.replace('box','');
// no need to unbind, remove this
// $('.box').unbind('click');
$('#overlay').show();
$('#image' + theNumber).fadeIn();
$('#close').fadeIn();
$('#close').click( function () {
$('#overlay').fadeOut();
$('#image' + theNumber).fadeOut();
$('#close').fadeOut();
$box.removeClass('disabled'); // re-enable click
});
}
};
$(document).ready( function() {
$('.box').click(handler);
});
如果我是你,我会这样做:
var handler = function() {
var $box = $('.box');
if($box.hasClass('disabled')){
// do nothing, just disallow further spawns
} else{
$box.addClass('disabled');
var theIDval = $(this).attr('id');
var theNumber = theIDval.replace('box','');
// no need to unbind, remove this
// $('.box').unbind('click');
$('#overlay').show();
$('#image' + theNumber).fadeIn();
$('#close').fadeIn();
$('#close').click( function () {
$('#overlay').fadeOut();
$('#image' + theNumber).fadeOut();
$('#close').fadeOut();
$box.removeClass('disabled'); // re-enable click
});
}
};
$(document).ready( function() {
$('.box').click(handler);
});
看起来您所缺少的只是在
\close
单击处理程序函数中重新绑定.box
单击事件
$('#close').click(function() {
$('#overlay').fadeOut();
$('#image' + theNumber).fadeOut();
$('#close').fadeOut();
$('.box').click(handler);
});
看起来您缺少的只是重新绑定
.box
单击事件(位于#close
单击处理程序函数中)
$('#close').click(function() {
$('#overlay').fadeOut();
$('#image' + theNumber).fadeOut();
$('#close').fadeOut();
$('.box').click(handler);
});
这也行得通,但另一个答案更简洁。谢谢你的帮助!这也行得通,但另一个答案更简洁。谢谢你的帮助!