简体   繁体   English

如何在另一个div jQuery中显示基于内容的div

[英]how can I show a div based on content in another div jQuery

I have multiple divs in html such: 我在html中有多个div,例如:

<div class="litter"> 
    <div class="litter-1">
        <div class="status">Available</div> 
        <div class="available"><img /></div>
    </div>
    <div class="litter-2">
        <div class="status">Available</div> 
        <div class="available"><img /></div>
    </div>
</div>

The status text will vary based on user input. 状态文本将根据用户输入而有所不同。 If the status is available the available class should not show. 如果状态为可用,则不应显示可用的类。 But if the status is unavailable then it should. 但是,如果状态不可用,则应该。 The image is present all the time but only displaying if the status changes. 图像始终显示,但仅在状态更改时显示。

I can get the jQuery to either hide all of the images, or show them all, but not based on the html value of the status. 我可以使jQuery隐藏所有图像或全部显示,但不基于状态的html值。

jQuery jQuery的

if($('.litter > .status').html()==="Available") {
    $(this).next('.available').hide();
    } else {
    $('.available').show();
}

Any help? 有什么帮助吗?

You could use a loop using jQuery .each() : 您可以使用jQuery .each()使用循环:

 // change selector to '.litter .status' $('.litter .status').each(function() { // loop through each .status element // get partner img container .available var imgContainer = $(this).parent().find('.available'); if ($(this).text() === "Available") { imgContainer.hide(); } else { imgContainer.show(); } }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> <div class="litter"> <div class="litter-1"> <div class="status">Available</div> <div class="available"><img src="https://via.placeholder.com/150/" alt="placeholder1" /></div> </div> <div class="litter-2"> <div class="status">Other status</div> <div class="available"><img src="https://via.placeholder.com/150/" alt="placeholder2" /></div> </div> </div> 

You can toggle a single class, status-available , and combine it with the adjacent sibling combinator to control the display of the image. 您可以切换status-available的单个类,并将其与相邻的同级组合器组合以控制图像的显示。

<!-- Image will show -->
<div class="status status-available">…</div>

vs.

<!-- Image will not show -->
<div class="status">…</div>

Example

 /* Image is hidden by default */ .available > img { display: none; } /* Adjacent sibling combinator in action */ .status-available + .available > img { display: block; } 
 <div class="litter"> <div class="litter-1"> <div class="status status-available">Available</div> <div class="available"> <img src="http://placekitten.com/150/150" alt="Cute cat" /> </div> </div> <div class="litter-2"> <div class="status">Available</div> <div class="available"> <img src="http://placekitten.com/150/150" alt="Cute cat" /> </div> </div> </div> 

Ok, so Haldo put me on the right track, but in order to make it all come together with the string. 好的,Haldo使我走上了正确的轨道,但为了使它们与弦一起出现。 Instead of if ($(this).text() === ("Available") I had to use this if ($(this).text().indexOf('Needs a Forever Home') > -1) 代替if ($(this).text() === ("Available")我不得不使用if ($(this).text().indexOf('Needs a Forever Home') > -1)

The rest of the code stays the same as Haldo's. 其余代码与Haldo相同。

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

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