使用 Jquery close() 函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1346972/
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 11:15:52  来源:igfitidea点击:

Using the Jquery closest() function?

closestjquery

提问by Nick

Here's my HTML:

这是我的 HTML:

<tr>
    <td class="show_r3c">click here</td>
</tr>
<tr class="r3c">
    <td>This is what I'd like to display</td>
</tr>

And I have currently got this JQuery code,

我目前有这个 JQuery 代码,

    $(document).ready(function(){
        $(".r3c").hide();        
        $('.show_r3c').click(function() { 
                         $(this).closest('.r3c').toggle(); 
                         return false; 
        });
   });

For some reason the closest()function isn't working, and it won't toggle the table row .r3c- I've tried using parent and other alternatives but can't get it working either :(

由于某种原因,该closest()功能不起作用,并且它不会切换表格行.r3c-我尝试使用父级和其他替代方法,但也无法使其正常工作:(

Apologies for the silly question, and something similar to a problem I've had before. Just wondering, What's the best solution for this ?

为这个愚蠢的问题道歉,以及与我以前遇到的问题类似的问题。只是想知道,对此的最佳解决方案是什么?

Thanks!

谢谢!

回答by Damo

closest() finds the closest parent not the parents-siblings-children.

最接近的()找到最近的父母而不是父母 - 兄弟姐妹 - 孩子。

You want something like:

你想要这样的东西:

$(document).ready(function(){
    $(".r3c").hide();

    $('.show_r3c').click(function() { 
        $(this).closest('table').find("tr.r3c").toggle(); return false; 
    });
});

回答by CMS

Try with:

尝试:

$('.show_r3c').click(function() {
  $(this).parent('tr').next('tr.r3c').toggle();
  return false;
});

回答by stefita

perhaps this would work:

也许这会奏效:

$(document).ready(function(){
    $(".r3c").hide();

    $('.show_r3c').click(function() { 
        $(this).parent().siblings(":first").toggle(); 
        return false; 
    });
});