如何通过getElementById选择“td标签”

时间:2013-02-27 07:56:23

标签: html

<script>

    alert(document.getElementById('a'));

</script>
<html>
    <table>
    <tr>
     <td id='a' class="test">test</td>
    </tr>
    </table>
</html>

我尝试了这个,但结果得到了“null”。 谁有人可以帮忙? 感谢〜

2 个答案:

答案 0 :(得分:3)

试试这个

<html>
    <table>
    <tr>
     <td id='a' class="test">test</td>
    </tr>
    </table>

<script>

    alert(document.getElementById('a'));

</script>
</html>

<td>下面有脚本标记。 Null是因为你试图在执行脚本时不存在。

答案 1 :(得分:1)

<html>
<head>
    <script>
        var readyStateCheckInterval = setInterval(function() {
            if (document.readyState === "complete") {
                alert(document.getElementById('a'));
                clearInterval(readyStateCheckInterval);
            }
        }, 10);
    </script>
    </head>
    <body>
        <table>
        <tr>
         <td id='a' class="test">test</td>
        </tr>
        </table>
    </body>
</html>

document.readyState是所有浏览器内置的属性,用于检查页面是否已加载。

有关readyState属性的更多信息: