使用jquery隐藏<a> tags if no description

时间:2018-03-12 18:12:35

标签: jquery

I am trying to hide tags when they have no text description, for a set of dynamically generated links. Usually this won't be necessary as links that don't have text to click on will not be visible anyway. But the page uses a stylesheet which assigns borders to each of these links. So my aim is to hide these links unless they contain clickable text (because their borders are visible)

I need a formula to target the text in the a tag - to say if the tag has no text example -> [a href=""] ...?..[/a] then it should be hidden.

$(document).ready(function() {
  $(".l3_subMenu  > a").hide();
  $(".l3_subMenu > a").each(function(i) {
    if ($(".l3_subMenu > a") == '') {
      $(".l3_subMenu > a").show();
    }
  });
});
l3_subMenu a {
  border: 1px solid #ccc;
  display: inline;
  padding: 0.4em 2em 0.4em 2em;
  margin-left: 1em;
  line-height: 3.0;
  background-color: #ffffff;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<div class="L3Links l3_subMenu">
  <a href="#Section1Title">Pre employment checks</a>
  <a href="#Section2Title">&nbsp;</a>
  <a href="#Section3Title">&nbsp;</a>
  <a href="#Section4Title">&nbsp;</a>
  <a href="#Section5Title">&nbsp;</a>
</div>

2 个答案:

答案 0 :(得分:1)

试试这个:

$(document).ready(function() {
  $(".l3_subMenu  > a").filter(function() {
    return $(this).text().trim() == "" 
  }).hide()
});

它会隐藏所有空的链接,也就是说不包含任何文本。

<强>演示

$(document).ready(function() {
  $(".l3_subMenu  > a").filter(function() {
    return $(this).text().trim() == "" 
  }).hide()
});
l3_subMenu a {
  border: 1px solid #ccc;
  display: inline;
  padding: 0.4em 2em 0.4em 2em;
  margin-left: 1em;
  line-height: 3.0;
  background-color: #ffffff;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="L3Links l3_subMenu">
  <a href="#Section1Title">Pre employment checks</a>
  <a href="#Section2Title">&nbsp;</a>
  <a href="#Section3Title">&nbsp;</a>
  <a href="#Section4Title">&nbsp;</a>
  <a href="#Section5Title">&nbsp;</a>
</div>

答案 1 :(得分:0)

我建议每个人切换

&#13;
&#13;
Team Name: New York Excelsior
Wins: 14
&#13;
$(function() {
  $(".l3_subMenu  > a").each(function() {
    $ele = $(this);
    $ele.toggle($ele.text().trim() !== "");
  });
});
&#13;
l3_subMenu a {
  border: 1px solid #ccc;
  display: inline;
  padding: 0.4em 2em 0.4em 2em;
  margin-left: 1em;
  line-height: 3.0;
  background-color: #ffffff;
}
&#13;
&#13;
&#13;