代码之家  ›  专栏  ›  技术社区  ›  Deepika Rao

如果id不可用并且是子元素,则为变量赋值

  •  0
  • Deepika Rao  · 技术社区  · 7 年前

    如果id可用,则为:

    var table = document.getElementById("myTable");
    

    <table>
        <tbody>
            <tr>
                <td>
                </td>
            </tr>
            <tr id="nested-table">
                <td>
                    <table> <!--need to assign this as the variable-->
                        <tbody>
                            <tr>
                                <td>
                                </td>
                            </tr>
                        </tbody>
                    </table>    
                </td>
            </tr>
        </tbody>
    </table>
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Alberto Trindade Tavares    7 年前

    您可以使用以下选择器:

    var table = document.querySelector("#nested-table table");