简体   繁体   English

如果Javascript中不存在其他div,如何隐藏div

[英]How to hide div if other divs not exists in Javascript

I'm trying to hide the div title if related divs are no present:如果相关的 div 不存在,我会尝试隐藏 div 标题:

Main HTML structure:主要HTML结构:

<div class="row parent">
    <div id="title-1" class='col-12 prov-title'>
        <h2>$category->name</h2>
    </div>
    <article id="child-11" class="col-md-6 mb-4 child">
        ... Content ...
    </article>
    <div id="title-2" class='col-12 prov-title'>
        <h2>$category->name</h2>
    </div>
    <article id="child-21" class="col-md-6 mb-4 child"> 
        ... Content ...
    </article>
    <article id="child-22" class="col-md-6 mb-4 child"> 
        ... Content ...
    </article>
    <div id="title-3" class='col-12 prov-title'>
        <h2>$category->name</h2>
    </div>
    <article id="child-31" class="col-md-6 mb-4 child"> 
        ... Content ...
    </article>
    <article id="child-32" class="col-md-6 mb-4 child"> 
        ... Content ...
    </article>
    <article id="child-33" class="col-md-6 mb-4 child"> 
        ... Content ...
    </article>
    ...
</div>

Filtering the content I get:过滤我得到的内容:

<div class="row parent">
    <div id="title-1" class='col-12 prov-title'>
        <h2>$category->name</h2>
    </div>
    <article id="child-11" class="col-md-6 mb-4 child">
        ... Content ...
    </article>
    <article id="child-12" class="col-md-6 mb-4 child"> 
        ... Content ...
        </article>
    <div id="title-2" class='col-12 prov-title'>
        <h2>$category->name</h2>
    </div>
</div>

I mean, I need to hide the #title-2 div if #child-21 and #child-22 layers are not shown.我的意思是,如果没有显示#child-21 和#child-22 层,我需要隐藏#title-2 div。

I understand that you would have to make a loop and look for .child pages by ID that start with the same number as the corresponding title div and hide it.我知道您必须创建一个循环并按 ID 查找以与相应标题 div 相同的数字开头的 .child 页面并将其隐藏。 I don't know much about the wildcards in the references to the id...我对 id 引用中的通配符了解不多...

What would be the best way to do it in javascript?在javascript中最好的方法是什么?

Thnaks,纳克斯,

Loop through the titles, and convert the title's ID to the prefix of the corresponding children.遍历标题,并将标题的 ID 转换为相应子项的前缀。 Then check if there are any elements with that kind of ID, and hide or show the title depending on it.然后检查是否有任何具有该 ID 的元素,并根据它隐藏或显示标题。

 document.querySelectorAll(".prov-title").forEach(title => { let child_prefix = title.id.replace(/^title-/, 'child-'); if (document.querySelector(`.child[id^="${child_prefix}"]`)) { title.style.display = 'block'; } else { title.style.display = 'none'; } });
 <div class="row parent"> <div id="title-1" class='col-12 prov-title'> <h2>Category 1</h2> </div> <article id="child-11" class="col-md-6 mb-4 child"> ... Content 11 ... </article> <article id="child-12" class="col-md-6 mb-4 child"> ... Content 12 ... </article> <div id="title-2" class='col-12 prov-title'> <h2>Category 2</h2> </div> </div>

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

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