【问题标题】:jQuery $(this).closest show & hidejQuery $(this).closest 显示和隐藏
【发布时间】:2013-08-02 19:51:39
【问题描述】:

我有一个包含多行的表格。每行都有一个显示详细信息按钮和一个隐藏详细信息按钮。单击显示详细信息时,我希望隐藏详细信息按钮仅显示特定行。我认为 .closest() 函数可以工作,但还没有。

这里是 HTML

<table>
    <tr id="1">
      <td><button class='view'>View Details</button><button class='hide' style='display:none;'>Hide Details</button></td>
    </tr>

    <tr id="2">
      <td><button class='view'>View Details</button><button class='hide' style='display:none;'>Hide Details</button></td>
    </tr>
</table>

这里是 jQuery

$(".view").click(function() {   
    $("#patient").show("");
    $(this).hide();
    $(this).closest(".hide").show();
});

【问题讨论】:

    标签: jquery


    【解决方案1】:

    .closest 只查看当前元素及其父元素。你必须这样做:

    $(this).parent().find('.hide');
    

    $(this).siblings('.hide');
    

    【讨论】:

      【解决方案2】:

      在 dom 准备好之后你绑定了吗?试试这个:

      $(function(){
          $(".view").click(function() {   
              $("#patient").show("");
              $(this).hide().next().show();
          });
      });
      

      【讨论】:

        【解决方案3】:

        尝试使用 next() 而不是最接近的()。

        http://api.jquery.com/next/ http://api.jquery.com/siblings/

        使用最接近时,您通常会给它一些额外的信息,例如要查找的类或其他信息。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2019-02-19
          • 2021-10-19
          • 2017-03-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多