jQuery $(this).closest 显示和隐藏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18025106/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 20:24:12  来源:igfitidea点击:

jQuery $(this).closest show & hide

jquery

提问by Chris Olson

I've got a table with multiple rows. In each row is a Show Details Button and a Hide Details button. On clicking show details I want the hide details button to only show for the specific row. I thought the .closest() function would work but it hasn't yet.

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

Here is the HTML

这是 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>

Here is the jQuery

这是jQuery

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

采纳答案by Prasanth

Are you binding after the dom is ready? Try this:

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

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

回答by kalley

.closestlooks at the current element and its parents only. You'd have to do this like:

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

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

or

或者

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

回答by Nick

try next() instead of closest().

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

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

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

When using closest, you usually give it some additional info like a class to look for, or something else.

使用最接近时,您通常会为其提供一些附加信息,例如要查找的类或其他信息。