首页 > 解决方案 > 如何遍历表并让 td 元素遵循条件

问题描述

我只是想让它在不符合要求tr时隐藏起来td,尝试使用 jQuery 和 JavaScript,不知道出了什么问题。

$(document).ready(function(){
    $("td").each(function() {
        var id = $(this).attr("price_search");
        if (id > value4 && id < value5) {
            $(this).hide;
        }
        else {
            $(this).hide;
        }
    });         
});

标签: javascriptjqueryhtmlloopshtml-table

解决方案


你可以这样做。

希望这会帮助你。

$(document).ready(function() {
  var value4 = 2;
  var value5 = 4;
  $("td").each(function() {
    var id = $(this).attr("price_search");
    if (id > value4 && id < value5) {
      $(this).hide();
    } else {
      $(this).show();
    }
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
  <tr>
    <td price_search="3">10</td>
    <td price_search="2">20</td>
    <td price_search="3">30</td>
  </tr>
</table>


推荐阅读