[英]How do I hide an html element on a specific page that already has the style (visibility: visible) and only if the child has a certain class
I have content/posts that are shown in a grid on one webpage from which a user can choose and subsequently be taken to that specific content on another page.我有内容/帖子显示在一个网页的网格中,用户可以从中选择,然后被带到另一页上的特定内容。 I have the same grid of content/posts on six different pages.
我在六个不同的页面上有相同的内容/帖子网格。 On each page I want to filter out content based on a specific class.
在每一页上,我想根据特定的 class 过滤掉内容。
Each item in the grid is already styled "visible" I suspect by the plugin I'm using.网格中的每个项目都已经被我使用的插件设置为“可见”的样式。
In the example below, the class I want to use to filter the content is "ld_topic_category-video-interaction-a1".在下面的示例中,我想用来过滤内容的 class 是“ld_topic_category-video-interaction-a1”。 The element I want to hide is the specific "item grid-1" which is connected to "ld_topic_category-video-interaction-a1".
我要隐藏的元素是连接到“ld_topic_category-video-interaction-a1”的特定“item grid-1”。
<div class="item grid-1" style="visibility: visible;">
<article id="post-2637" class="post post-2637 sfwd-topic type-sfwd-topic status-publish hentry ld_topic_category-video-interaction-a1 user-has-not-earned"...</article>
</div>
I've tried the following code:我试过以下代码:
jQuery(document).ready(function($) {
$('ld_topic_category-video-interaction-a1').each(function(){
if($(this).hasClass("ld_topic_category-video-interaction-a1")){
$(this).parent().css("display", "none");
}
});
});
As well as:也:
jQuery(document).ready(function($) {
$('ld_topic_category-video-interaction-a1').each(function(){
if($(this).hasClass("ld_topic_category-video-interaction-a1")){
$(this).parent().hide();
}
});
});
And this:和这个:
var elements = document.getElementsByClassName('ld_topic_category-video-interaction-a1');
for(let i=0; i< elements.length; i++){
elements[i].style.display = "none";
}
Also this:还有这个:
jQuery(document).ready(function($) {
$('.item.grid-1').each(function(){
if($(this).hasClass("ld_topic_category-video-interaction-a1")){
$(this).parent().css("display", "none");
}
});
});
Any suggestions would be greatly appreciated.任何建议将不胜感激。 Please let me know if there is any other info I need to provide.
如果我需要提供任何其他信息,请告诉我。
Thanks谢谢
CSS Visibility and Display properties are not the same. CSS 可见性和显示属性不一样。 Please refer to the link below: https://www.tutorialrepublic.com/css-tutorial/css-visibility.php#:~:text=CSS%20Visibility%20vs,affect%20the%20layouts .
请参考以下链接: https://www.tutorialrepublic.com/css-tutorial/css-visibility.php#:~:text=CSS%20Visibility%20vs,affect%20the%20layouts 。
If you want to stick with visibility style you can do this instead to hide the element:如果你想坚持可见性风格,你可以这样做来隐藏元素:
$(this).parent().css("visibility", "visible"); //this is to make element visible again
$(this).parent().css("visibility", "hidden"); //this is to make element hidden
Refer to this answer for more info on CSS Visibility: Equivalent of jQuery.hide() to set visibility: hidden有关 CSS 可见性: 等效于 jQuery.hide() 设置可见性的更多信息,请参阅此答案:隐藏
If you wanna stick with your jquery code however, you can instead make do with Display property:但是,如果您想坚持使用 jquery 代码,则可以使用 Display 属性:
<div class="item grid-1" style="display:block;"> //to show the element
<article id="post-2637" class="post post-2637 sfwd-topic type-sfwd-topic status-publish hentry ld_topic_category-video-interaction-a1 user-has-not-earned"...
</article>
</div>
<div class="item grid-1" style="display:none;"> //to hide the element
<article id="post-2637" class="post post-2637 sfwd-topic type-sfwd-topic status-publish hentry ld_topic_category-video-interaction-a1 user-has-not-earned"...
</article>
</div>
I hope this helps.我希望这有帮助。
could you use display: none to hide the items of the grid you don't want?您可以使用 display: none 隐藏您不想要的网格项目吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.