javascript - 使用 jquery 检索父节点的子节点

标签 javascript jquery nodes

我有一个带有这种形式的 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")

最佳答案

这两个都行

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);  
  };            
};    

关于javascript - 使用 jquery 检索父节点的子节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20218451/

相关文章:

javascript - 如何在 Angular JS 条件下显示一个元素

Javascript 总是在最长的数组上运行一个函数,并在该函数中使用最短的

jquery - 如何使用 AJAX 仅从前几个 XML 条目获取数据

javascript - Ajax、asp.net mvc3 路由和相关 url

javascript - 突出显示背景颜色淡入/淡出的区域

javascript - 使用 jQuery 进行图像预览

javascript - AngularJS ng-repeat jQuery find 仅返回一个元素

orientation - Graphviz:控制节点在子图中对齐

java - 遍历二叉搜索树以找到所有叶子

java - 使用递归方法返回链表中具有最大值的对象