jQuery仅将父div内的元素作为目标 [英] jQuery target only elements within parent div

查看:78
本文介绍了jQuery仅将父div内的元素作为目标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在页面上有许多小部件,并且我想在整个过程中重复使用类.我有一个问号图标,可以在小部件内的2个div之间切换可见性.

问题是我无法让我的代码仅以当前小部件为目标.我尝试了通常的技术,但似乎没有一个在这里起作用.

小提琴在这里, http://jsfiddle.net/clintongreen/9hvVn/

有问题的单击功能可通过问号图标运行.需要toggleClass的div是.widget-content和.widget-content_next

谢谢:)

解决方案

widget-content和widget-content_next不是链接的直接祖先,我想您想要这样的东西:

 $(this).closest(".widget").find('.widget-content').toggleClass("hidden");
 $(this).closest(".widget").find(".widget-content_next").toggleClass("hidden");

I have a number of widgets on a page and I want to re-use classes throughout. I have a question mark icon that should toggle visibility between 2 divs inside the widget.

The problem is I can't get my code to only target the current widget. I have tried the usual techniques but none seem to be working here.

The fiddle is here, http://jsfiddle.net/clintongreen/9hvVn/

The click function in question works from the question mark icon. The divs that need toggleClass are .widget-content and .widget-content_next

Thanks :)

解决方案

widget-content and widget-content_next aren't direct ancestors of the link, I think you want something like this:

 $(this).closest(".widget").find('.widget-content').toggleClass("hidden");
 $(this).closest(".widget").find(".widget-content_next").toggleClass("hidden");

这篇关于jQuery仅将父div内的元素作为目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆