Jquery:如果元素中存在类则隐藏元素
if( ('.container').closest('.item7') = true){
hide THE container of item7.
//----
//need help with this line
//something like $closest('.item7').('.container').hide();
//----
}
摘要:
如果容器中存在 item7 类,则隐藏容器。
if( ('.container').closest('.item7') = true){
hide THE container of item7.
//----
//need help with this line
//something like $closest('.item7').('.container').hide();
//----
}
Summary:
if item7 class exists in container hide THE container.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是一个小提琴,显示它正在工作> http://jsfiddle.net/ULZGH/
Here's a Fiddle, showing it working > http://jsfiddle.net/ULZGH/
目前尚不完全清楚您想要做什么,但多次阅读您的问题后,我猜测您想要这样的逻辑。如果
.container
对象中的任何位置都有.item7
类的对象,则隐藏.container
父对象。如果这就是您真正想要做的,您可以这样做:这将找到
.container
中的任何.item7
对象,然后隐藏最接近的每个匹配的.container
父对象。如果有一个
.container
不包含.item7
,则不会被触及。如果.item7
不在.container
中,则不会触及它。如果可能有多个级别的容器,并且您想将它们全部隐藏,您可以这样做:
这会找到所有
.item7
对象,然后隐藏所有.container
父对象。我最喜欢的做法实际上是这样的。如果只有几个
.container
对象,但有很多.item7
对象,这会更有效:这表示要找到每个
.container
目的。然后,在生成的 jQuery 对象(所有.container
对象中)中,删除任何没有后代.item7
的.container
对象,并然后隐藏剩余的.container
对象。It's not entirely clear what you're trying to do, but after reading your question multiple times, my guess is that you want logic like this. If the
.container
object has an object in it anywhere with the.item7
class, then hide the.container
parent object. If that's what you're really trying to do, you can do it like this:This will find any
.item7
objects that are in a.container
and then hide the closest.container
parent object of each match.If there's a
.container
that does not contain a.item7
, it will not be touched. If there's a.item7
that is not in a.container
, it will not be touched.If there could be multiple levels of containers and you wanted to hide them all, you could do it like this:
This finds all
.item7
object and then hides any.container
parents.My preferred way of doing it is actually this. This would be more efficient if there were only a few
.container
objects, but lots of.item7
objects:This says to find each
.container
object. Then in that resulting jQuery object (of all.container
objects), remove any.container
object that don't have a descendant.item7
and then hide the reminaing.container
objects.如果你有更多容器,你应该循环它们:)
If you have more containers, you should loop them :)
简单:
simple :