0
我有一个全屏Google地图,然后我有一个弹出窗口。我想要的是弹出消失,当我点击任何东西,但弹出或如果我按esc。取消绑定jquery事件只能工作一次
下面的代码只能运行一次。我可以打开弹出窗口并关闭它,但不会再打开。
var hideBlogContent = function() {
$(document).on('click', function (e) {
if ($(e.target).closest($("#blogpost")).length === 0) {
$("#blogpost").hide();
$(document).unbind();
$(document).off();
}
});
$(document).on('keydown', function (e) {
if (e.keyCode === 27) { // ESC
$("#blogpost").hide();
$(document).unbind();
$(document).off();
}
});
我该如何让它每次都能工作?
为什么你要解绑事件呢? –
,这样我就可以再次打开弹出窗口。否则会在打开它时隐藏帖子? –
您可能想要将函数包装在相应的包装函数中,并在需要时调用它,如果您仍想每次解除绑定但确保只解除当前事件(如$(document).unbind('click'); – DinoMyte