我有简单的脚本,当弹出div显示时,它会在背景上创建图层。
jQuery(".openForm").click(function() {
jQuery("#popup").show();
jQuery("body").append('<div id="layer"></div>');
});
这样可以正常工作但是,当我点击某处时,它应该用这个脚本关闭弹出窗口
jQuery("#layer").click(function() {
jQuery("#popup").hide();
jQuery("#layer").remove();
});
无论如何不发生,甚至没有错误。
答案 0 :(得分:3)
当您尝试绑定处理程序时,我猜测#layer
不存在。改为使用事件委托:
jQuery(document).on('click', "#layer", function() {
jQuery("#popup").hide();
jQuery("#layer").remove();
});
或者,您可以隐藏/显示#layer
div(如#popup
div),而不是每次都添加和删除它。