Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery toggle()仅需要表行_Jquery - Fatal编程技术网

Jquery toggle()仅需要表行

Jquery toggle()仅需要表行,jquery,Jquery,我有一个从php循环生成的表,所以切换的行具有相同的类。当我单击切换以显示一条记录的详细信息时,它切换到所有其他记录 这有点令人困惑。所以我试着在这里创建一个 A先生 记录1。。。。 记录2。。。。 B先生 记录1。。。。 $(文档).ready(函数(){ $(“.details”).hide(); $(“.showit”)。单击(函数(){ $(“.details”).toggle(); }); }); 问题是,当我点击显示A先生的记录时,它也会显示B先生的记录。有什么解决方案吗?非常感

我有一个从php循环生成的表,所以切换的行具有相同的类。当我单击切换以显示一条记录的详细信息时,它切换到所有其他记录

这有点令人困惑。所以我试着在这里创建一个


A先生
记录1。。。。
记录2。。。。
B先生
记录1。。。。
$(文档).ready(函数(){
$(“.details”).hide();
$(“.showit”)。单击(函数(){
$(“.details”).toggle();
});
});

问题是,当我点击显示A先生的记录时,它也会显示B先生的记录。有什么解决方案吗?非常感谢…

您可以捕获单击的行,并检索包含类详细信息的所有后续行:

$(".showit").click(function(){
    $(this).closest("tr") // Retrieves the tr that was clicked
           .nextUntil(":not(.details)") // Gets all the following tr's that have the detail class
           .toggle();
});

更新的fiddle:

非常流畅-直到现在才听说nextUntil()。干得好,也没有想过使用
nextUntil()
。嗯,我想
$(".showit").click(function(){
    $(this).closest("tr") // Retrieves the tr that was clicked
           .nextUntil(":not(.details)") // Gets all the following tr's that have the detail class
           .toggle();
});