只是在用Javascript作爲一些方法來處理一些cometishian代碼時,我該如何爲這段代碼構造一個構造函數呢?下面的代碼是無效的:getElementById的結果是否爲空?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<link rel="Stylesheet" href="gStyle.css" />
<script type="text/javascript" language="javascript">
// Gantt chart object
function ganttChart(gContainerID) {
this.isDebugMode = true;
this.gContainer = document.getElementById(gContainerID);
if (this.isDebugMode) {
this.gContainer.innerHTML += "<div id=\"gDebug\">5,5 | 5.1</div>";
}
}
var myChart = new ganttChart("chart1");
</script>
</head>
</html>
<body>
<div id="chart1" class="gContainer"></div>
</body>
</html>
this.gContainer是空
可能重複(http://stackoverflow.com/questions/8739605/getelementbyid-returns-null) – 2012-04-07 22:54:41