我想在對話框中顯示註冊/登錄窗體(lightbox樣式),但兩者僅在點擊觸發器超鏈接時顯示一次。點擊一次後,頁面仍會模糊,但對話框不會顯示任何內容。jQuery - append()在使用empty()後不能再次工作()
當省略empty()函數時,此代碼正常工作,但登錄和註冊表單都顯示在1對話框中。當用戶點擊登錄鏈接時,我只想顯示登錄表單,當用戶點擊登記超鏈接時,我只想顯示登記表格。
請參閱下面的代碼(HTML,CSS,jQuery)。
<html>
<head>
<style>
#lightbox {
position:fixed;
top:0;
left:0;
width:100%;
height:100%;
background:rgba(0,0,0,0.5);
display:none;
}
#invisible_register, #invisible_login {
display:none;
position:absolute;
}
</style>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
jQuery(document).ready(function($) {
$('.trigger_register').click(function(e) {
e.preventDefault();
$('#lightbox').empty().append($('#invisible_register'));
$('#lightbox').show();
$('#invisible_register').show();
});
$('.trigger_login').click(function(e) {
e.preventDefault();
$('#lightbox').empty().append($('#invisible_login'));
$('#lightbox').show();
$('#invisible_login').show();
});
//Click anywhere on the page to get rid of lightbox window
$("#lightbox").click(function() {
$('#lightbox').hide();
});
//Except for the dialog box
$(".dialog").click(function(e) {
e.stopPropagation();
return false;
});
});
</script>
</head>
<body>
<div id="lightbox"></div>
<div id="invisible_register">
<div class="container">
<div class="dialog">
<div class="box">
<div class="box_left">
<h1>Register</h1>
</div>
<div class="box_right">
<div class="error_text"></div>
</div>
<div class="clear"></div>
</div>
</div>
</div>
</div>
<div id="invisible_login">
<div class="container">
<div class="dialog">
<div class="box">
<div class="box_left">
<h1>Login</h1>
</div>
<div class="box_right">
<div class="error_text"></div>
</div>
<div class="clear"></div>
</div>
</div>
</div>
</div>
<a href="" class="button trigger_register">Register</a>
<a href="" class="button trigger_login">Login</a>
</body>
</html>
或參閱本搗鼓這個問題的一個簡單的例子:http://jsfiddle.net/zwprf0yw/
編輯 克隆()函數的效果很好,但是這會導致另一個問題:該對話框被關閉點擊時它。我認爲它阻止了這塊代碼的工作。有什麼建議麼?
$(".dialog").click(function(e) {
e.stopPropagation();
return false;
});
感謝你的答案。 clone()函數運行良好,但是這會導致另一個問題:單擊它時,對話框現在會關閉。 (看我編輯的問題) – guy1997 2014-09-28 21:49:35
@ guy1997 [這裏是修改過的小提琴。](http://jsfiddle.net/Lrev6doz/1/)您必須使用委託事件處理才能使其工作。 – Pointy 2014-09-28 22:41:57