我有一系列重复的div,内容通过jQuery注入。顺便说一句,父div的数量是动态的,因此可以是随机的。
在每个div中,我有一个触发模态窗口的锚点(H3 a)。我的问题是,每次点击目标元素时,注入父div的每个部分都会触发模态窗口。
所以,我的问题是,如何隔离每个父母,以便只有当前的一个触发模态,而不必过度使用.eq()?
这是我的脚本片段,触发模态窗口:
//prepend modal window to positive message container
jQuery('.rateRibbon').each(function () {
jQuery(this).append('<div class="modelessWindow hideWindow">' + modWin1 + '</div>');
});
//remove class to show modeless window
jQuery('.posMsg a.showWindow').click(function (e) {
jQuery('.modelessWindow.hideWindow').removeClass('hideWindow');
});
//add class to hide modeless window
jQuery('.modelessWindow a.closeModWin').click(function (e) {
jQuery('.modelessWindow').addClass('hideWindow');
});
//when .roomTypeLineItem is clicked add class to hide nested modal window
jQuery('.roomTypeLineItem').click(function (e) {
jQuery('.modelessWindow').addClass('hideWindow');
});
使用整个脚本链接到我的Fiddle。
提前感谢您的帮助!
答案 0 :(得分:0)
制作你的第二个jQuery块:
jQuery('.posMsg a.showWindow').click(function (e) {
jQuery(this).closest('.rateRibbon').find('.modelessWindow.hideWindow').removeClass('hideWindow');
});
<强> jsFiddle example 强>
这使得点击相对于元素,并且只会选择适当的模态。