我有一个带有这种形式的 html 表格:
<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")
最佳答案
这两个都行
$(function() {
$(".edit").on("click",function() {
var $thWithKeyTd = $("th[idth='keyth']");
var $parentChild = $(this).closest("table").find("th[idth='keyth']");
});
});
纯 JS
window.onload=function() {
document.querySelector(".edit").onclick=function() {
var thWithKeyTd = document.querySelector("th[idth='keyth']");
console.log(thWithKeyTd.innerHTML);
};
};
关于javascript - 使用 jquery 检索父节点的子节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20218451/