简体   繁体   English

如果父div没有子项显示,则显示某个不同的子div

[英]If parent div has no children showing then show a certain different child div

I am seeing simlar questions about hiding parent divs if there is no child but can't find how to show a different div in the parent if no other child is in it. 我看到类似的问题:如果没有孩子,则隐藏父div,但是如果其中没有其他孩子,则找不到如何在父中显示不同的div。

I have a parent div that is updated with free meeting rooms: 我有一个父div,该div已更新为免费会议室:

.Parent{
    width: 100%;
    margin-top: 4px;
    overflow: auto;
}

if there is a free room it is display on the board (in the parent). 如果有空房间,它会显示在板上(在父级中)。 This is done in JS like so: 这是在JS中完成的,如下所示:

$('#Parent').addClass("showRooms");    

If a room is not free by default it is hidden: 如果默认情况下房间不是空闲的,那么它将被隐藏:

if(roomStatus == "Taken"){
    $('#Parent').addClass("hideRooms");    
}

The css classes are as so: CSS类是这样的:

.showRooms{
    visibility: visible;
    background-color: green;
}
.hideRooms{
    visibility:hidden;
}

When all the rooms are hidden there is a blank board, I would like to show a different child div in the parent so I can show something more interesting eg the company logo. 当所有房间都隐藏后,有一块空白板,我想在父级中显示一个不同的子div,以便显示一些更有趣的内容,例如公司徽标。

(I am aware I could have the compnay logo on the parent even if there are rooms showing but I only want it to show if there are no rooms free) (我知道即使有房间显示,我也可以在父母身上贴上compnay徽标,但我只希望它在没有房间的情况下显示)

What can I use to achieve this? 我可以用什么来实现这一目标?

Yes! 是!

I've came up with a pure CSS solution, because combining selectors is awesome: 我提出了一个纯CSS解决方案,因为组合选择器非常棒:

Consider the following setup: 请考虑以下设置:

 .container { margin: 10px; border: 1px solid #000; } .room { width: 100px; height: 75px; background-color: #F00; } .hidden { display: none; } .placeholder { display: block; } .room:not(.hidden) ~ .placeholder { display: none; } 
 <div class="container"> <div class="room hidden"></div> <div class="room hidden"></div> <div class="room hidden"></div> <div class="room hidden"></div> <div class="placeholder">No rooms available!</div> </div> <div class="container"> <div class="room hidden"></div> <div class="room"></div> <div class="room"></div> <div class="room hidden"></div> <div class="placeholder">No rooms available!</div> </div> 

Now the magic lies in the following lines: 现在,魔术就在于以下几行:

.room:not(.hidden) ~ .placeholder {
    display: none;
}

Explanation: 说明:

Take a placeholder, who is a sibling of a .room that does not contain the .hidden class. 以一个占位符为例,该占位符是不包含.hidden类的.room的同级对象。 The placeholder is visible by default, but if it can find a sibling that has a .room without .hidden, it will fall back into display none. 默认情况下,占位符是可见的,但是如果找到具有 .room且没有.hidden的同级兄弟,它将退回到不显示任何内容。

Take note, this requires the placeholder div to always be the last child of it's parent. 请注意,这要求占位符div始终是其父级的最后一个子级。 Since the ~ selector only checks for next siblings, not previous. 由于~选择器仅检查下一个同级,而不检查上一个。

I would go something like : 我会像这样:

if(allRoomStatusAreTaken()){
  $('#Parent').addClass("showLogo");
} else {
  $('#Parent').removeClass("showLogo");
}

And

.showLogo{
  visibility: visible;
  background-image: url(...);
}

In allRoomStatusAreTaken() you have to check if all rooms are taken. allRoomStatusAreTaken()您必须检查是否已占用所有房间。 I would use a function like every from Lodash : 我会使用Lodash的 every函数:

function allRoomStatusAreTaken() {
  return every(allRooms, room => room.status === "Taken");
}

You could hide the logo by default, and change the display using js if the rooms are hidden. 您可以默认隐藏徽标,如果隐藏了房间,则可以使用js更改display Example: 例:

 $(function() { var roomStatus = "Taken"; if (roomStatus == "Taken") { $('#Parent').addClass("hideRooms"); $('.logo').addClass('show'); } }) 
 .Parent { width: 100%; margin-top: 4px; overflow: auto; } .showRooms { visibility: visible; background-color: green; } .hideRooms { visibility: hidden; } .logo { display: none; width: 200px; height: 200px; background: red; } .logo.show { display: block; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="Parent"> <div class="logo"> </div> </div> 

Just Keep the Logo div with class 'companyLogo' inside parent and use the following CSS and will work. 只需将Logo div的类为'companyLogo'保留在父类中,并使用以下CSS即可使用。

.hideRooms .companyLogo{
visibility:visible;
}
.showRooms .companyLogo{
visibility:hidden;
}

For more specific answer please provide HTML structure. 有关更具体的答案,请提供HTML结构。

When the parent is free you have to use append to add anything you want to the parent 当父母空闲时,您必须使用附加将所需的任何内容添加到父母

if(roomStatus == "Taken"){
$('#Parent').addClass("hideRooms");    
$("#Parent").append("<span>somthing to show</span>");
}

You can have a logo wrapped in some div (or anything else, or you can add a class to the logo image, really anything), which will have a 'hidden' class by default which will hide it and then you can also show this whet you have no rooms, something like: 您可以将徽标包裹在某些div中(或其他任何名称,或者可以在徽标图像中添加一个类,实际上什么都可以),默认情况下,该类将具有一个“隐藏”类,将其隐藏,然后您也可以显示此类如果您没有房间,例如:

if(roomStatus == "Taken") {
    $('#Parent').addClass("hideRooms");
    $('.logo').addclass("visible");
    $('.logo').removeClass("hidden");
} else {
    $('#Parent').addClass("showRooms");
    $('.logo').removeClass("visible");
    $('.logo').addClass("hidden");
}

``` ```

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

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