简体   繁体   English

如何在已经具有样式的特定页面上隐藏 html 元素(可见性:可见)并且仅当孩子具有某个 class

[英]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.

相关问题 当某个幻灯片在fullpage.js滑块中具有活动类别时,如何隐藏元素? - How do I hide an element when a certain slide has a class of active in fullpage.js slider? javascript:仅在父元素处于活动状态且子元素具有特定类的情况下,才如何向子元素添加属性? - javascript: how to add an attribute to a child element ONLY if a parent element is active AND child element has a certain class? 仅当子容器具有内容时,如何将类添加到特定的父元素 - How do I add a Class to Specific Parent Elements ONLY if the Child Container has Content 仅当列表项具有某个 class 时才显示子元素 - Only show a child element when list item has a certain class 如何选择仅具有特定类别的元素? - How can I select an element which has only a specific class? 如何使用JavaScript在特定InnerHTML上没有ID的类HTML元素上创建ID? - How do I create ID on a class HTML element on specific InnerHTML has no ID, with JavaScript? 如果页面上的其他元素具有某个类,则如何向该元素添加类 - How to add class to an element if a different element on the page has a certain class 如何隐藏具有给定类的所有元素的父元素,但父元素也具有特定的类? - How can I hide parent element of all elements with a given class, but the parent element also has a specific class? Jquery:如何检查元素是否具有某些css类/样式 - Jquery: How to check if the element has certain css class/style 如何检测类中的哪个元素具有特定样式 - How to detect which element in a class has a certain style
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM