简体   繁体   English

如果子p标签为空,则隐藏div块

[英]Hide div blocks if child p tags are empty

I'm working on a vertical timeline and I have three categories of events: national, international and obituaries, each of which is specified by classes of the same name. 我正在垂直时间轴上工作,我将事件分为三类:国家事件,国际事件和ob告事件,每个事件都由同名的类指定。 The structure of a div block is as follows: div块的结构如下:

<div class="cd-timeline__block js-cd-block">
<div class="cd-timeline__img cd-timeline__img--location js-cd-img"> 
<img src="img/cd-icon-location.svg" alt="Location">
</div>
<div class="cd-timeline__content js-cd-content">
<p class="National List">The Election Commission recommends to the President the disqualification of 20 AAP MLAs in Delhi</p>           
<span class="cd-timeline__date">Jan. 19</span></div>

If the user selects 'National' events in a dropdown (which is part of the UI), a Javascript functions automatically hides the other two category events like this: 如果用户在下拉菜单(属于UI的一部分)中选择“国家”事件,则Javascript函数会自动隐藏其他两个类别事件,如下所示:

$('#newscategory').on('change', function(){
        if(this.value=="Nat"){
            $('.National').show();
            $('.International').hide();
            $('.Obituaries').hide();
        }
});

However, if there are no 'National' events present on a particular date, the container surrounding the p tags does not get hidden. 但是,如果在特定日期不存在“全国性”事件,则围绕p标签的容器不会被隐藏。 I want the timeline to skip that date altogether, which means I want to hide the parent div with class="cd-timeline__block js-cd-block" if the p tags are hidden. 我希望时间轴完全跳过该日期,这意味着如果p标签被隐藏,我想使用class =“ cd-timeline__block js-cd-block”隐藏父div。 How can I achieve this? 我该如何实现? Another problem is that there are hundreds of events on the page. 另一个问题是页面上有数百个事件。

An example of the code can be found here 可以在此处找到代码示例

Iterate through parent blocks using .each , and check if its child paragraph element has any content, if so leave it, if no content within child paragraph elements, hide it. 使用.each遍历父块,并检查其子段落元素是否包含任何内容,如果保留,则保留它,如果子段落元素中没有内容,则将其隐藏。

    $('.cd-timeline__block.js-cd-block').each(function () {

        var showElement = false;

        $(this).find('p').each(function () {
            var len = $.trim($(this).html()).length;

            if (len > 0) {
                showElement = true;
            }
        });

        if (showElement === false) {
            $(this).hide();
        } else {
            $(this).show();
        }
    });

This way, parent block would appear if at least one p child element has any content available. 这样,如果至少一个p子元素具有任何可用内容,则将出现父块。

Could make use of .toggle , Array.some and :empty : 可以使用.toggleArray.some:empty

 $(function() { // For each div element $('div').each(function () { // Show it only if one of its children paragraphs is non-empty $(this).toggle($(this).find('p').toArray().some(function (p) { return !$(p).is(':empty'); })); }); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div> div with some non empty paragraphs <p>non empty</p> <p></p> <p>non empty</p> </div> <div> div with only empty paragraphs <p></p> <p></p> <p></p> </div> 

In your case, replace $('div') with a proper (more specific) selector. 在您的情况下,用适当的(更具体的)选择器替换$('div')

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

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