2013-11-26 3 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» вылетающих из класса редактирования. Exemple:

$(".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']"); 

    });    
});  

Plain 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? Closeest(). GetAttribute ('')? – user222914

+0

getAttribute не jQuery – mplungjan

0

Вот простой ванили JS вкус и это быстрее, чем решение JQuery

"th_id" : this.offsetParent.querySelector('[keyth]') 
Смежные вопросы