首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >jQuery:只针对“当前”div

jQuery:只针对“当前”div
EN

Stack Overflow用户
提问于 2014-06-04 15:04:25
回答 1查看 60关注 0票数 0

我有一系列重复的div,内容通过jQuery注入。顺便说一下,父div的数量是动态的,因此可以是随机的。

在每个div中,我有一个锚(H3 a)来触发一个模态窗口。我的问题是,每次单击目标元素时,注入父div的每个区段都会触发模态窗口。

因此,我的问题是,如何隔离每个父级,以便只使用当前的模式,而不必过度使用.eq()?

下面是触发模态窗口的脚本片段:

代码语言:javascript
复制
//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');
});

还有一个链接到我的小提琴和整个脚本。

提前感谢您的帮助!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-06-04 18:16:49

使用第二个jQuery块:

代码语言:javascript
复制
jQuery('.posMsg a.showWindow').click(function (e) {
    jQuery(this).closest('.rateRibbon').find('.modelessWindow.hideWindow').removeClass('hideWindow');
});

jsFiddle实例

这使得单击相对于元素,并且只会选择适当的模式。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24041024

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档