Javascript 使用jquery检索父节点的子节点
我有一个包含以下表单的html表:Javascript 使用jquery检索父节点的子节点,javascript,jquery,nodes,Javascript,Jquery,Nodes,我有一个包含以下表单的html表: <html> <table> <thead> <tr> <th idth='keyth'></th> </tr> </thead> <tbody> <tr> <td class='edit' idtd='keytd'&
<html>
<table>
<thead>
<tr>
<th idth='keyth'></th>
</tr>
</thead>
<tbody>
<tr>
<td class='edit' idtd='keytd'></td>
</tr>
</tbody>
</table>
</html>
谢谢你的建议
我终于找到了一个只使用Javascript的解决方案:
"th_id":this.parentNode.parentNode.parentNode.childNodes[0].childNodes[0].childNodes[0].getAttribute("idth")
这两种方法都有效
普通JS
这是一个简单的香草JS风格,它比jQuery解决方案更快
"th_id" : this.offsetParent.querySelector('[keyth]')
我想我必须做3次parent()和3次children(),但它不起作用。如何使用getAttribute实现最接近?最接近().getAttribute(“”)?getAttribute不是Jquery我想我需要一个只使用Javascript的解决方案。在我浪费时间使用Jquery之前,我正在混合Jquery和Javascript有用的信息。你可以使用QuerySelector,然后看看我的答案。
$(function() {
$(".edit").on("click",function() {
var $thWithKeyTd = $("th[idth='keyth']");
var $parentChild = $(this).closest("table").find("th[idth='keyth']");
});
});
window.onload=function() {
document.querySelector(".edit").onclick=function() {
var thWithKeyTd = document.querySelector("th[idth='keyth']");
console.log(thWithKeyTd.innerHTML);
};
};
"th_id" : this.offsetParent.querySelector('[keyth]')