這可能是一個不雅的做法,但是...我正在用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),它會在點擊時關閉。有任何想法嗎?