How to console.log a <td> that has an ID. Getting ReferenceError: document is not defined

How to console.log a <td> that has an ID. Getting ReferenceError: document is not defined

差不多就是标题。某处可能有解决方案,但我太笨了,无法正确表达。很抱歉,如果是这样的话。

<head>
  <script src="javascript.js"></script>
</head>

<table>
    <tbody>
        <tr>
            <td id="test">Some random, text separated, by commas, that I will, try to separate, with filter.split(/, /);, sometime in the future </td>
        </tr>
    </tbody>
</table>
var testTd = document.getElementById('test');
console.log(testTd);

我可以做到吗 console.log(testTd) 输出是: "Some random, text separated, by commas, that I will, try to separate, with filter.split(/, /);, sometime in the future "

一直出现 "ReferenceError: document is not defined" 错误。

将你的脚本标签放在正文的末尾而不是头部

这应该适用于 jquery:

$("#test").text();

或者你也可以使用这个:

document.getElementById ( "tdid" ).innerText