总有一天你的谜底会解开 2022-03-10 13:50 采纳率: 77.4%
浏览 31
已结题

js Jquery得不到元素的子节点

为什么得不到id为head标签的子标签th?
下面代码直接复制可运行。
求解

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
    <script >
        function println(){
            let Nodes=$("#head").childNodes;
            console.log(Nodes);
        }
    </script>
    <title>Title</title>
</head>

<body>
<table>
    <thead>
    <tr id="head">
        <th>
            <input  type="checkbox"/>
        </th>
        <th>name</th>
        <th>author</th>
        <th>store</th>
        <th>money</th>
        <th>date</th>
    </tr>
    </thead>
    <tbody id="tbody">

    </tbody>
</table>
<button onclick="println()">打印</button>
</body>
</html>
  • 写回答

3条回答 默认 最新

  • 崽崽的谷雨 2022-03-10 14:17
    关注

    $("#hwad").children("th")

    $("#head").find("th")
    children和find的区别:children只会查找直接子集,而find会跨越层级查找,一直找到没有为止

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 3月18日
  • 已采纳回答 3月10日
  • 创建了问题 3月10日