jQuery-隐藏

时间:2018-10-06 03:30:11

标签: javascript jquery html

如果我的爱好没有价值(空),我想隐藏。但是,如果业余爱好仍然有价值,那么仍然会显示出来。如何调节呢?我尝试使用jQuery。

$("tr:last-child td:last-child").css("font-weight","bold")

if($("tr:last-child td:last-child").length < 1){
	$("tr:last-child").hide()
}
table{
  border: 1px solid blue;
  padding: 4px 8px;
  margin:4px 0
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr>
    <td>Name</td>
    <td>John</td>
  </tr>
  <tr>
    <td>Hobby</td>
    <td>Sleeping</td>
  </tr>
</table>

<table>
  <tr>
    <td>Name</td>
    <td>Doe</td>
  </tr>
  <tr>
    <td>Hobby</td>
    <td></td>
  </tr>
</table>

3 个答案:

答案 0 :(得分:2)

如果td的{​​{3}}为空白,则可以隐藏.parent() $("tr:last-child td:last-child").each(function(index, td) { if($(td).text() === ""){ $(td).parent().hide(); } });

table {
  border: 1px solid blue;
  padding: 4px 8px;
  margin:4px 0
}
tr:last-child td:last-child {
  font-weight: bold;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr>
    <td>Name</td>
    <td>John</td>
  </tr>
  <tr>
    <td>Hobby</td>
    <td>Sleeping</td>
  </tr>
</table>
<table>
  <tr>
    <td>Name</td>
    <td>Doe</td>
  </tr>
  <tr>
    <td>Hobby</td>
    <td></td>
  </tr>
</table>
document.createElement()

答案 1 :(得分:1)

您需要使用text()来获取td的文本

$("tr:last-child td:last-child").each(function(index,element){
    $(element).css("font-weight","bold");
});
$("tr:last-child td:last-child").each(function(index,element){
    if($.trim($(element).text()).length == 0){
       $(element).parent().hide();
    }
});
table{
  border: 1px solid blue;
  padding: 4px 8px;
  margin:4px 0
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr>
    <td>Name</td>
    <td>John</td>
  </tr>
  <tr>
    <td>Hobby</td>
    <td>Sleeping</td>
  </tr>
</table>

<table>
  <tr>
    <td>Name</td>
    <td>Doe</td>
  </tr>
  <tr>
    <td>Hobby</td>
    <td></td>
  </tr>
</table>

答案 2 :(得分:0)

更改此内容:

if($("tr:last-child td:last-child").length < 1){
    $("tr:last-child").hide()
}

收件人:

if($("tr:last-child td:last-child").text().length < 1){
    $("tr:last-child").hide()
}