2013-11-26 49 views
2

我有一個HTML表,此表:檢索父節點的childNode使用jQuery

<html> 
<table> 
    <thead> 
     <tr> 
      <th idth='keyth'></th> 
     </tr> 
    </thead> 
    <tbody> 
     <tr> 
      <td class='edit' idtd='keytd'></td> 
     </tr> 
    </tbody> 
</table> 
</html> 

現在我想在jQuery來檢索屬性「keyth」從編輯類離開的內容。 例:

$(".edit") { 

    "th_id": this.parentNode.....childNode.getAttribute("idth"), 
    //Here I dont know how to achieve the attribute content 

}); 

謝謝您的建議!

我終於發現只有JavaScript的解決方案:

"th_id":this.parentNode.parentNode.parentNode.childNodes[0].childNodes[0].childNodes[0].getAttribute("idth") 
+0

看一看我的回答更快。 – redV

回答

0

這兩個工作

Live Demo

$(function() { 
    $(".edit").on("click",function() { 
    var $thWithKeyTd = $("th[idth='keyth']"); 

    var $parentChild = $(this).closest("table").find("th[idth='keyth']"); 

    });    
});  

平原JS

Live Demo

window.onload=function() { 
    document.querySelector(".edit").onclick=function() { 
    var thWithKeyTd = document.querySelector("th[idth='keyth']"); 
    console.log(thWithKeyTd.innerHTML); 
    };    
};  
+0

doesn'twork我認爲我必須做3次父()和3次子(),但它不起作用 – user222914

+0

getAttribute與nearest()。getAttribute('')? – user222914

+0

getAttribute不是jQuery – mplungjan

0

這是一個普通的香草味JS和它比jQuery的解決方案

"th_id" : this.offsetParent.querySelector('[keyth]')