簡體   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