简体   繁体   English

如何获得被点击的表格行的TBODY?

[英]How can I get a clicked table row's TBODY?

I have a table with a few rows in it. 我有一张桌子,上面有几行。 Each row has an onclick event that is supposed to check the ID of the tbody element. 每行都有一个onclick事件,该事件应检查tbody元素的ID。 This is stored in a variable for later use in a function. 它存储在变量中,以供以后在函数中使用。

Right now I have this snippet of jQuery: 现在,我有这个jQuery片段:

var parentTable = $(this.parentNode)[0].id;

However, this only gets the ID of the entire table, not the tbody . 但是,这仅获取整个表的ID,而不是tbody

What's the best way to specify the ID of the tbody element? 指定tbody元素ID的最佳方法是什么?

First, your use of jQuery is wasted there. 首先,您对jQuery的使用在那里浪费了。 It would be written like this: 它会这样写:

var parentTable = this.parentNode.id;

As far as getting the tbody id, assuming this is actually the row, your code should do it. 就获取tbody id而言,假设this实际上是该行,则您的代码应该这样做。

If you're actually getting the table, then that would be very unusual. 如果您实际上正在得到桌子,那将是非常不寻常的。 The only way a tbody would not get created would be if you manually created the table from DOM creation methods, and left it out. 无法创建tbody的唯一方法是,如果您是通过DOM创建方法手动创建表的,则将其遗漏了。

您可能希望将该信息存储在每一行的data-tbody-id中,然后,当用户单击该行时,事件对象将保存该信息。

您可以使用以下功能

var tobody = parentTable.children('tbody')

You can use the following function 您可以使用以下功能

var tbody = $(this).parents("tbody");
var id = tbody.attr("id");

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

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