这是我的代码:
<?php
$nameArray = array(
array("Andy", "Jackson"),
array("Barry", "Obama"),
array("Benny", "Franklin"),
array("Georgey", "Washington"),
array("Billy", "Clinton"))
?>
<script type="text/javascript">
function shout(msg) {
console.log(msg);
}
window.onload=initiate()
function initiate(){
shout("initiate 0");
var nameTable = document.getElementById("xyzTable");
shout ("initiate 1 - nameTable is "+nameTable);
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Blablabla</title>
</head>
<body>
<table id='xyzTable'>
<?php
foreach ($nameArray as $pair){
echo
"<tr>
<td>".$pair[0]."</td>
<td>".$pair[1]."</td>
</tr>";
}
?>
</table>
</body>
</html>
当我这样做时,JS 变量的内容nameTable
是null
,正如我在 Web 控制台中看到的那样。这是为什么?