繁体   English   中英

获取父母的属性

[英]Getting the attribute of a parent

我正在尝试使用表格行获取表格的ID。 当我处于循环中时:

$(this).parent.attr('id');

但是我遇到一个错误。 我的语法正确吗?

谢谢。

编辑:

<table id="21-rawTable" border="1"><tbody><tr class="even notSelected eClr" name="0" id="beaf-32;Developmental-Stage_Embryo-0-12h;ChIP-chip;Rep-1;input;Dmel_r54;modENCODE_21;BEAF_Input_0.CEL" title="/modencode/modencode-dcc/symbolic_links/Dmel_r5.4/Non-TF-Chromatin-binding-factor/ChIP-chip/raw-arrayfile_CEL/beaf-32/Embryo 0-12h/" style="cursor:pointer;" "="" onclick="selected('beaf-32;Developmental-Stage_Embryo-0-12h;ChIP-chip;Rep-1;input;Dmel_r54;modENCODE_21;BEAF_Input_0.CEL','[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]')"><td class="checkbox"><input type="checkbox" id="beaf-32;Developmental-Stage_Embryo-0-12h;ChIP-chip;Rep-1;input;Dmel_r54;modENCODE_21;BEAF_Input_0.CELcheckBox" checked=""></td><td>beaf-32;Developmental-Stage_Embryo-0-12h;ChIP-chip;Rep-1;input;Dmel_r54;modENCODE_21;BEAF_Input_0.CEL</td><td>0 bytes</td></tr><!-- table--></tbody></table>

使用此代码获取表的ID(在本例中为“ 21-rawTable”)

$(".selected").each(function () {
var parentname = $(this).parent().attr('id');
        alert (parentname);
});

我解决了这个问题。 我认为这给了我直接的父母。 相反,我不得不像这样搜索最接近的祖先:

var parentname = ($(this).closest('table')).attr('id');

谢谢。

parent [ API Ref ]是一种方法,因此您应该这样调用它:

$(this).parent().attr('id');

我认为您需要在对parent()函数的调用中包含括号。

在您的示例HTML中,没有看到带有class='selected'标签。 然而:

  • 复选框的父项是TD。
  • TD的父级是TR。
  • TR的父级是TBODY。
  • TBODY的父级是TABLE。

但是,您可以使用.closest(selector)从其中的任何地方获取表祖先:

$(this).closest('table').attr('id');

http://api.jquery.com/closest/

暂无
暂无

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

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