简体   繁体   English

如何在给定特定条件后隐藏标签?

[英]How to hide a tag after given speciffic condition?

I have this Handlebars template:我有这个 Handlebars 模板:

<div class="card-body">          
  <h5 class="card-title"><i class="fa fa-users" aria-hidden="true"></i> {{ this.group }}
    <i class="fa fa-user" aria-hidden="true"> </i> {{ this.name }} {{ this.lastName }}
  </h5>
  <a href="https://wa.me/{{ this.phone }}"><i class="fa fa-whatsapp" aria-hidden="true"></i> {{ this.phone }}</a><br>
  <i class="fa fa-home" aria-hidden="true"></i> {{ this.address }} <br>
  <a href="mailto:{{ this.email }}"><i class="fa fa-envelope" aria-hidden="true"></i> {{ this.email }}</a> 
  <br>          
  <hr>
  <p class="card-text">  
    <a class="facebook" href="https://www.facebook.com/{{ this.facebook }}" > <i class="fa fa-facebook" aria-hidden="true"></i></a>
    <a href="https://www.instagram.com/{{ this.instagram }}" ><i class="fa fa-instagram" aria-hidden="true"></i></a>
    <a href="https://twitter.com/{{ this.twitter }}"><i class="fa fa-twitter" aria-hidden="true"></i></a>
    <a href="https://www.linkedin.com/in/{{ this.linkedIn }}"><i class="fa fa-linkedin" aria-hidden="true"></i></a><br>                
  </p>
</div>

And I want to hide all "a" tags that won't get a "href" value.我想隐藏所有不会获得“href”值的“a”标签。

I've tried this, for instance-我试过这个,例如-

<script>                           
    if (!document.getElementsByClassName("facebook").value) {      
      $(".facebook").hide();
    }    
</script>

But it hides all "a" tags, no matter if the comply with the condition or not.但它隐藏了所有“a”标签,无论是否符合条件。 I'm guessing that it must be something to do with iterate each generated tag, but I can't figure out how to target them.我猜这一定与迭代每个生成的标签有关,但我不知道如何定位它们。

You can to it using each :您可以使用each

$("a").each(function() {
    if($(this).attr("href") == "") {
        $(this).hide();
    }
});

Learn more: https://api.jquery.com/each/了解更多: https : //api.jquery.com/each/

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

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