我在外部文件中有一个 JavaScript 数组:
<script src="assets/players.js"></script>
contents of above array:
window.players = [
"Addison, Jackson",
"Agron, Dmitry",
"Zuccarino, John"
]
我尝试返回记录数但无济于事:
<h3>Sample data file with <script>window.players.length;</script> names:</h3>
我做错了什么......?
最佳答案
如果你想写出长度,那么你需要使用 document.write
<h3>Sample data file with <script>document.write(window.players.length)</script> names:</h3>
或者您可以使用 DOM 方法。
<h3>Sample data file with <span id="count"></span> names:</h3>
<script>
document.querySelector("#count").innerHTML = window.players.length;
</script>
关于javascript数组长度无法在网页中访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45645548/