总有一天你的谜底会解开 2022-03-17 11:29 采纳率: 77.4%
浏览 25
已结题

由js建立的表格,无法通过jquery选择器选择到

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
    <script>
        function select(){
            let x=$("td input:checked").parent().parent();
            let xx=x.children();
            console.log("选中:");
            console.log(xx);
        }
        function begin(){
            console.log("执行begin函数了");
            let tbody=document.getElementById("tbody");
            for(let x=0;x<3;x++){//行
                let w=document.createElement("tr");
                let input=document.createElement("input");
                input.setAttribute("type","checkbox");
                w.append(input);
                for(let q=0;q<2;q++){//列
                    let ww=document.createElement("td");
                    ww.innerHTML=" "+x;
                    w.appendChild(ww);
                }
                tbody.append(w);
            }

        }
    </script>
</head>
<body onload="begin()">
<table contenteditable="true">
    <thead>
    <tr id="head">
        <th>
            <input  type="checkbox"/>
        </th>
        <th>name</th>
        <th>author</th>
    </tr>
    </thead>
    <tbody id="tbody" >
<!--        <tr id="tr1">-->
<!--            <td>-->
<!--                <input  type="checkbox"/>-->
<!--            </td>-->
<!--            <td>-->
<!--                2-->
<!--            </td>-->
<!--            <td>-->
<!--               2-->
<!--            </td>-->
<!--        </tr>-->
<!--        <tr id="tr2">-->
<!--            <td>-->
<!--                <input  type="checkbox"/>-->
<!--            </td>-->
<!--            <td>-->
<!--                1-->
<!--            </td>-->
<!--            <td>-->
<!--                1-->
<!--            </td>-->
<!--        </tr>-->
<!--        <tr id="tr3">-->
<!--            <td>-->
<!--                <input  type="checkbox"/>-->
<!--            </td>-->
<!--            <td>-->
<!--                3-->
<!--            </td>-->
<!--            <td>-->
<!--                3-->
<!--            </td>-->
<!--        </tr>-->
    </tbody>
</table>
<button onclick="select()">选择出被选中的</button>
</body>
</html>

我想在控制台输出被选中的td,但是发现由js生成表格时,好像读不到任何关于生成的东西。
若不使用begin函数生成把表格内容,而使用html中静态的表格内容时,就可以通过select函数读取到td。
这是为什么?

  • 写回答

2条回答 默认 最新

  • 你好!机器人 2022-03-17 11:46
    关注

    是tr

    function select(){
                let x=$("tr input:checked").parent().parent();
                let xx=x.children();
                console.log("选中:");
                console.log(xx);
            }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

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

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装