开发者

hide heading if no div below

开发者 https://www.devze.com 2023-01-15 20:14 出处:网络
I have some layers like so <div class=\"evenprop\">some prop</div> <div class=\"evenprop\">some prop2</div>

I have some layers like so

<div class="evenprop">some prop</div>
<div class="evenprop">some prop2</div>
<div class="evenprop">some prop3</div>

<h3 class="otherprop">Other Props</h3>

<div class="evenprop">some prop4</div>
<div class="evenprop">some prop5</div>

This is fine but I need to hide the "otherprop" class if the data looks like thi开发者_Go百科s

<div class="evenprop">some prop</div>
<div class="evenprop">some prop2</div>
<div class="evenprop">some prop3</div>
<div class="evenprop">some prop4</div>
<div class="evenprop">some prop5</div>

<h3 class="otherprop">Other Props</h3>

The layers are dynamically put in so not sure how to do this I somehow need to say if there is no data below the class"otherprop" then hide "otherprop" if that makes sense

The desired result if no data below is

<div class="evenprop">some prop</div>
<div class="evenprop">some prop2</div>
<div class="evenprop">some prop3</div>
<div class="evenprop">some prop4</div>
<div class="evenprop">some prop5</div>

but if there is data below

<div class="evenprop">some prop</div>
<div class="evenprop">some prop2</div>
<div class="evenprop">some prop4</div>
<div class="evenprop">some prop5</div>

<h3 class="otherprop">Other Props</h3>

<div class="evenprop">some prop3</div>

Many thanks

Jamie


try something like this

var nxtSib = document.getElementsByClassName('otherprop')[0].NextSibling;
if(nxtSib != 'evenprop')
    nxtSib.style.display = 'none';


Try this in your CSS:

.otherprop:last-child {
  display: none;
}


Whenever you dynamically change the data there, you could check if any such things are there, like so...

var op = $('.otherprop');
if(op.nextAll('div').length) {
    op.show();
else {
    op.hide();
}

Instead of using op.show() and op.hide(), I'd also recommend using op.addClass('available') and op.removeClass('available') with the following accompanying css

.otherprop {
    display: none;
}
.otherprop.available {
    display: block;
}

Untested, but should work.


Thanks for the answers but this is how I got my desired result in the end

if ($('.property > .evenprop').is(':visible')){
            $('.otherprop').show();
        } else {
            $('.otherprop').hide();
        }
0

精彩评论

暂无评论...
验证码 换一张
取 消