这可能是一个不雅的做法,但是...我正在用jQuery制作自己的lightbox窗口脚本 - 这是为了学习目的而不是其他任何事情。jQuery .append()
使用这个脚本:
$(document).ready(function() {
$(".popout").hide();
$(".modal-links a").each(function(i){
$(this).click(function(){
$("body").append('<div class="overlay"></div>')
$(".popout").append('<a class="close" href="#">Close</a>')
var modal = $(".popout").eq(i),
modalWidth = modal.width(),
modalHeight = modal.height();
modal.css({
"width":modalWidth,
"height":modalHeight,
"margin-left":-(modalWidth/2)})
.show().siblings(".popout").hide();
});
});
$(".close").click(function(){
$(".popout").hide();
$(".overlay").hide();
});
});
我能够追加一类.close的锚链接。这是有效的,当我查看源代码时,它会按照我的意愿放入标记中。但是,在下面的函数中,我尝试定位这个附加类,并使用一个单击事件来触发模态窗口的隐藏,并在背景中覆盖。这不起作用。然而,如果我只是将a class="close"
标记复制到我的实际标记中(没有JS),它会在点击时关闭。有任何想法吗?