简体   繁体   English

通过单击td切换表格行

[英]table row toggle by clicking td

Please check the fiddle link http://jsfiddle.net/mv7Y5/89/ 请检查小提琴链接http://jsfiddle.net/mv7Y5/89/

am trying to achieve to toggle the table row by clicking the td, which is not happening please help needed. 我正试图通过单击td来切换表格行,但这种情况并未发生,请提供帮助。

    $(function() {
    $(".details").hide();
    $('.show-details').click(function(e) {
        $(this).next("tr").slideToggle(500);
    });
});

You are calling next() on the td that gets clicked (on the first cell, anyway). 您正在被单击的td上调用next() (无论如何在第一个单元格上)。 What next() does is it finds the nearest sibling of the element in question that matches the selector. next()作用是找到与选择器匹配的所讨论元素的最接近的同级元素。 There is no tr that is a sibling of the td that gets clicked. 没有tr是单击的td的同级。 You need to make sure that you're calling next() on the tr , so it can find the next tr : 您需要确保在tr上调用next() ,以便它可以找到下一个 tr

$(function() {
    $(".details").hide();
    $('td.show-details').click(function(e) {
        $(this).parent().next("tr").slideToggle(500);
    });
});

Additionally, your markup in that jsfiddle is a little screwy (you've got an inconsistent number of columns in your rows, for example, which I believe may be the result of some typos.) 此外,您在jsfiddle中的标记有些螺丝钉(例如,您的行中的列数不一致,例如,我认为这可能是某些拼写错误的结果。)

In your first row, you have the class name "show-details" on the "td". 在第一行中,“ td”上具有类名称“ show-details”。 On the subsequent rows, the class name is on the "tr" elements. 在随后的行中,类名称在“ tr”元素上。 Since you are using ".next()" to find a "tr", the class should be on the "tr" as in the second, third and forth rows. 由于您使用的是“ .next()”来查找“ tr”,因此该类应位于“ tr”上,就像第二,第三和第四行一样。

<tr class='rowToClick'><td>click me</td></tr>

Next, this should work: 接下来,这应该工作:

$(document).ready(function()
         {
             $(".rowToClick").click(function() { $(this).nextUntil(".rowToClick").toggle(); });
         });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM