简体   繁体   English

无法在 UL + LI (CSS) 中隐藏 HTML

[英]Unable to hide HTML within a UL + LI (CSS)

Can anyone see or think why I can't hide items 5 onwards?谁能看到或想到为什么我不能从第 5 项开始隐藏项目?

I would like to show ONLY items #1, #2, #3, and #4 with the rest all hidden (ie completely hidden from the code).我只想显示 rest 全部隐藏的项目#1、#2、#3 和#4(即完全隐藏在代码中)。

I can hide them from the client view but view source you can see the HTML - what am I doing wrong here?我可以从客户端视图中隐藏它们,但查看源代码你可以看到 HTML - 我在这里做错了什么?

I have tried:我努力了:

visibility: hidden 

As well as

display: none

And still, the code is there...仍然,代码在那里......

 ul>li { display: inline-block; /* You can also add some margins here to make it look prettier */ width: 180px; zoom: 1; *display: inline; /* this fix is needed for IE7- */ } ul>li:nth-of-type(1n+5) { display: none; }.speakercard { box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2); max-width: 180px; margin: auto; text-align: center; padding-top: 14px; } button { border: none; outline: 0; display: inline-block; padding: 10px; color: white; background-color: #393939; text-align: center; cursor: pointer; width: 100%; } a { text-decoration: none; color: black; } button:hover, a:hover { opacity: 0.7; }.image-cropper { width: 100px; height: 100px; position: relative; overflow: hidden; border-radius: 50%; border: 1px solid #ccc; margin-left: auto; margin-right: auto; }.profile-pic { display: inline; margin: 0 auto; height: 100%; width: auto; }
 <ul> <li> <div class="speakercard"> <div class="image-cropper"> <img src="./img.jpeg" class="profile-pic" alt="" style="width:100%"> </div> <p>1 Poo Doe</p> <p><button>Speaker Profile</button></p> </div> </li> <li> <div class="speakercard"> <div class="image-cropper"> <img src="./img.jpeg" class="profile-pic" alt="" style="width:100%"> </div> <p>2 Doe</p> <p><button>Speaker Profile</button></p> </div> </li> <li> <div class="speakercard"> <div class="image-cropper"> <img src="./img.jpeg" class="profile-pic" alt="" style="width:100%"> </div> <p>3 Doe</p> <p><button>Speaker Profile</button></p> </div> </li> <li> <div class="speakercard"> <div class="image-cropper"> <img src="./img.jpeg" class="profile-pic" alt="" style="width:100%"> </div> <p>4 Doe</p> <p><button>Speaker Profile</button></p> </div> </li> <li> <div class="speakercard"> <div class="image-cropper"> <img src="./img.jpeg" class="profile-pic" alt="" style="width:100%"> </div> <p>THIS SHOULD BE TOTALLY HIDDEN</p> <p><button>Speaker Profile</button></p> </div> </li> <li> <div class="speakercard"> <div class="image-cropper"> <img src="./img.jpeg" class="profile-pic" alt="" style="width:100%"> </div> <p>THIS SHOULD BE TOTALLY HIDDEN</p> <p><button>Speaker Profile</button></p> </div> </li> <li> <div class="speakercard"> <div class="image-cropper"> <img src="./img.jpeg" class="profile-pic" alt="" style="width:100%"> </div> <p>THIS SHOULD BE TOTALLY HIDDEN</p> <p><button>Speaker Profile</button></p> </div> </li> </ul>

Any idea how to make this happen?知道如何做到这一点吗?

Thanks!谢谢!

Demo to hide from also view source you should use ngIf directive rather than css演示要从查看源代码中隐藏,您应该使用ngIf指令而不是css

or you can use custom pipe to show what you want.或者您可以使用自定义pipe来展示您想要的内容。

or you can connect to ngFor your elements and filter your list related what you want.或者您可以连接到ngFor您的元素并过滤与您想要的内容相关的列表。

css just applied for design. css刚申请设计。 It does for user view它适用于用户视图

for ngIf your html对于 ngIf 你的 html

<ul>
  <ng-container *ngFor="let data of personels;let i = index;">
    <li *ngIf="i<4">
      <div class="speakercard">
        <div class="image-cropper">
          <img src="{{data.img}}" class="profile-pic" alt="" style="width:100%">
        </div>
        <p>{{data.id}} {{data.Name}}</p>
        <p><button>Speaker Profile</button></p>
      </div>
    </li>
  </ng-container>
</ul>

in component.ts just create your array在 component.ts 中只创建你的数组

personels=[
    {id:1,Name:"Poo Doe",img:"image1"},
    {id:2,Name:"Poo Doe",img:"image1"},
    {id:3,Name:"Poo Doe",img:"image1"},
    {id:4,Name:"Poo Doe",img:"image1"},
    {id:5,Name:"Poo Doe",img:"image1"},
    {id:6,Name:"Poo Doe",img:"image1"},
    {id:7,Name:"Poo Doe",img:"image1"}
  ]

and this is pipe example这是 pipe示例

create custom pipe创建自定义 pipe

import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
  name: 'customPipe'
})

export class CustomPipe implements PipeTransform {
  transform(row: any[],n:number): any {
      return row.filter((x,idx)=>idx<n);       
  }
}

and in html use pipe并在 html 中使用 pipe

<ul>
    <li *ngFor="let data of personels|customPipe:4">
      <div class="speakercard">
        <div class="image-cropper">
          <img src="{{data.img}}" class="profile-pic" alt="" style="width:100%">
        </div>
        <p>{{data.id}} {{data.Name}}</p>
        <p><button>Speaker Profile</button></p>
      </div>
    </li>
</ul>

Try this CSS:试试这个 CSS:

li {
  display: none;
}
li:nth-child(-n+3) {
  display: block;   
}

check on codepen检查代码笔

What you aim for is not possible with CSS, CSS is build just for styling the content of the DOM. CSS 无法实现您的目标,CSS 仅用于设置 DOM 内容的样式。 not for malipulating the latter, If you really want to remove the items after the speaker profile 4. then you would have to use JavaScript to do so.不是为了篡改后者,如果您真的想删除扬声器配置文件 4 之后的项目,那么您将不得不使用 JavaScript 来执行此操作。 JavaScript can manipulate the DOM on the client side, But why are you sending the profiles at all? JavaScript 可以在客户端操作 DOM,但是你为什么要发送配置文件呢? if you want to remove them afterwards?如果你想在之后删除它们? Wouldn't it be easier to just not send them to your clients?不将它们发送给您的客户不是更容易吗?

But if you rellay want to do so, here is how you can achieve your desired result.但是,如果您这样做,那么您可以通过以下方式实现您想要的结果。

 let speakercards = document.querySelectorAll('.speakercard'); for (let i = 0; i < speakercards.length; ++i) { if (i >= 4) { speakercards[i].remove() } }
 ul>li { display: inline-block; /* You can also add some margins here to make it look prettier */ width: 180px; zoom: 1; *display: inline; /* this fix is needed for IE7- */ } /* ul>li:nth-of-type(1n+5) { display: none; } */.speakercard { box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2); max-width: 180px; margin: auto; text-align: center; padding-top: 14px; } button { border: none; outline: 0; display: inline-block; padding: 10px; color: white; background-color: #393939; text-align: center; cursor: pointer; width: 100%; } a { text-decoration: none; color: black; } button:hover, a:hover { opacity: 0.7; }.image-cropper { width: 100px; height: 100px; position: relative; overflow: hidden; border-radius: 50%; border: 1px solid #ccc; margin-left: auto; margin-right: auto; }.profile-pic { display: inline; margin: 0 auto; height: 100%; width: auto; }
 <ul> <li> <div class="speakercard"> <div class="image-cropper"> <img src="./img.jpeg" class="profile-pic" alt="" style="width:100%"> </div> <p>1 Poo Doe</p> <p><button>Speaker Profile</button></p> </div> </li> <li> <div class="speakercard"> <div class="image-cropper"> <img src="./img.jpeg" class="profile-pic" alt="" style="width:100%"> </div> <p>2 Doe</p> <p><button>Speaker Profile</button></p> </div> </li> <li> <div class="speakercard"> <div class="image-cropper"> <img src="./img.jpeg" class="profile-pic" alt="" style="width:100%"> </div> <p>3 Doe</p> <p><button>Speaker Profile</button></p> </div> </li> <li> <div class="speakercard"> <div class="image-cropper"> <img src="./img.jpeg" class="profile-pic" alt="" style="width:100%"> </div> <p>4 Doe</p> <p><button>Speaker Profile</button></p> </div> </li> <li> <div class="speakercard"> <div class="image-cropper"> <img src="./img.jpeg" class="profile-pic" alt="" style="width:100%"> </div> <p>THIS SHOULD BE TOTALLY HIDDEN</p> <p><button>Speaker Profile</button></p> </div> </li> <li> <div class="speakercard"> <div class="image-cropper"> <img src="./img.jpeg" class="profile-pic" alt="" style="width:100%"> </div> <p>THIS SHOULD BE TOTALLY HIDDEN</p> <p><button>Speaker Profile</button></p> </div> </li> <li> <div class="speakercard"> <div class="image-cropper"> <img src="./img.jpeg" class="profile-pic" alt="" style="width:100%"> </div> <p>THIS SHOULD BE TOTALLY HIDDEN</p> <p><button>Speaker Profile</button></p> </div> </li> </ul>

It is my first answer on SO ever and guys above clearly gave you the solution but here is another way to accomplish the same result.这是我对 SO 的第一个答案,上面的人清楚地为您提供了解决方案,但这是实现相同结果的另一种方法。

let myobj = document.querySelectorAll("ul > li:nth-of-type(1n+5)");
Array.from(myobj).map((el)=>el.remove()) 

I would simply use ngClass directive:我会简单地使用 ngClass 指令:

 <ul>
  <li *ngFor="let user of users; let i = index" [ngClass]="{'hide': i>3}">
    <div class="speakercard">
      <div class="image-cropper">
        <img src="{{user.img}}" class="profile-pic" alt="" style="width:100%">
      </div>
      <p>{{user.id}} {{user.name}}</p>
      <p><button>Speaker Profile</button></p>
    </div>
  </li>
  </ul>

CSS: CSS:

.hide{
display:none;
}

You can also replace hide with bootstrap class d-none:您还可以将 hide 替换为 bootstrap class d-none:

<li *ngFor="let user of users; let i = index" [ngClass]="{'d-none': i>3}">

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

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