【发布时间】:2013-12-11 19:46:42
【问题描述】:
我希望弹出窗口一次只出现一个,无论有多少弹出窗口。因此,如果当前打开了一个弹出窗口,它将关闭,而新的弹出窗口将保持打开状态。我目前对它们进行了编码,因此初始链接将打开和关闭弹出窗口,并且用户应该能够单击文档中的任意位置以在打开弹出窗口后关闭它。
我有一个 jsfiddle here。
我在使用此代码时遇到的问题:
1.如果选择了另一个弹出窗口,则单击的第一个弹出窗口不会消失
2.如果您单击初始链接以外的任何位置将其关闭,则必须双击该链接以使弹出窗口重新出现
3.所有的弹窗都应该先关闭
HTML:
<div id="link"><a href="#" class="showPopup" rel="one"> One</a></div>
<div class="popup popup_hide" id="one">Content</div>
<div id="link"> <a href="#" class="showPopup" rel="two"> Two</a></div>
<div class="popup popup_hide" id="two">Content <a href="#">link</a></div>
<div id="link"> <a href="#" class="showPopup" rel="three"> Three</a></div>
<div class="popup popup_hide" id="three">Content <a href="#"></a></div>
Javascript:
jQuery(document).ready(function() {
var popupStatus = 0;
if (popupStatus == 0) { // if value is 0, show popup
$('.showPopup').click(function () {
$('#' + $(this).attr('rel') + '_img').removeClass('border_grey');
if ($(this).hasClass("selected")) {
$('#' + $(this).attr('rel')).hide();
$(this).removeClass("selected");
} else {
$(this).addClass("selected");
$('#' + $(this).attr('rel') + '_img').addClass('border_grey');
$('#' + $(this).attr('rel')).show();
popupStatus = 1;
}
return false;
});
}
else if (popupStatus == 1){
$('.popup').hide();
popupStatus = 0;
}
$('.hide_popup').click(function () {
$('img').removeClass('border_grey');
$('.popup').hide();
return false;
});
$(document).click(function (e) {
if (e.target.class !== 'popup_hide') {
$('.popup_hide').hide();
}
});
}); // jQuery结束
【问题讨论】:
标签: javascript jquery performance html popup