简体   繁体   English

如何隐藏基于一系列父元素的HTML元素

[英]How do you hide an HTML element based on a series of parent elements

What i am trying to accomplish is hiding all activity stream posts from one user in a feed by its data type. 我要完成的工作是按其数据类型在Feed中隐藏一个用户的所有活动流帖子。 I am able to distinguish the users post using CSS with this structure 我可以通过这种结构区分使用CSS的用户帖子

.joms-stream.joms-embedly--left.joms-js--stream > div > div > a > img[data-author="921"] {
display: none !important;

} }

How am i able to hide the entire stream item using a series of (this.parent) in jQuery or javascript? 我如何使用jQuery或javascript中的一系列(this.parent)隐藏整个流项目? Thank you in advance. 先感谢您。

Your problem can be solved by using the .parents(selector).last() code: 您的问题可以通过使用.parents(selector).last()代码解决:

For example: 例如:

$("img[data-author="921"]").parents(".joms-stream.joms-embedly--left.joms-js--stream")
    .last().hide();

This code works because jQuery returns a list of parents from .parent(selector) from in most to the out most, thus using .last() will select the out most parent. 这段代码之所以有效,是因为jQuery从.parent(selector)从最.parent(selector)到最外端返回一个父.parent(selector)列表,因此使用.last()将选择最.last()父级。

Or if you are sure that .joms-stream.joms-embedly--left.joms-js--stream won't appear twice in the nest (you should be quite sure because you are using > s to select), you can use the .closest(selector) method, which will select the closest match from the element: 或者,如果您确定.joms-stream.joms-embedly--left.joms-js--stream在嵌套中不会出现两次(您应该非常确定,因为您正在使用> s进行选择),则可以使用.closest(selector)方法,该方法将从元素中选择最接近的匹配项:

$("img[data-author="921"]").closest(".joms-stream.joms-embedly--left.joms-js--stream")
    .hide();

PS $(jqueryElem).hide() is equivalent to $(jQueryElem).style("display", "none") or elem.style.display = "none" because you seem to be quite new to jQuery. PS $(jqueryElem).hide()等同于$(jQueryElem).style("display", "none")elem.style.display = "none"因为您似乎对jQuery相当elem.style.display = "none"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM