1

这是我的代码:

<?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 变量的内容nameTablenull,正如我在 Web 控制台中看到的那样。这是为什么?

4

1 回答 1

10

initiate马上执行。我想你的意思是:

window.onload=initiate;

反而。

于 2013-03-12T22:47:48.193 回答