未捕获的TypeError:无法读取属性' lastChild'为null

时间:2016-11-22 19:12:11

标签: javascript html dom



<!DOCTYPE html>
<html>
<head>
	<title>Face Matching Game</title>
	<h2>Matching Game</h2>
	<p>Click on the EXTRA face on the left side</p>
	<style type="text/css">
		div { position: absolute; width: 640px; height: 550px; background-color: maroon }
		img { position: absolute; width: 100px; height: 100px}
		#rightSide { left: 650px; border-left: 2px solid black; }
	</style>
	<script type="text/javascript">
		var NUM_OF_FACES = 0;
		var THE_LEFT_SIDE, THE_RIGHT_SIDE;
		//Function to generate faces
		function generateFaces() {
			THE_LEFT_SIDE = document.getElementById("leftSide");
			THE_RIGHT_SIDE = document.getElementById("rightSide");
			NUM_OF_FACES += 5;
			var image;
			while(NUM_OF_FACES>0){
				image = document.createElement("img");
				image.src = "smile.png";
				image.style.top = Math.floor(Math.random()* (THE_LEFT_SIDE.offsetHeight - 100)) + "px";
				image.style.left = Math.floor(Math.random()* (THE_LEFT_SIDE.offsetWidth - 100)) + "px";
				THE_LEFT_SIDE.appendChild(image);
				NUM_OF_FACES -= 1;
				//console.log(image.style.top, image.style.left )
			}
			var leftSideImages = THE_LEFT_SIDE.cloneNode(true);
			leftSideImages.removeChild(leftSideImages.lastChild);
			THE_RIGHT_SIDE.appendChild(leftSideImages);
		}//end generatefaces()

		function deleteFaces(argument) {
			while(THE_LEFT_SIDE.firstChild){
				THE_LEFT_SIDE.removeChild(THE_LEFT_SIDE.firstChild);
			}
			while(THE_RIGHT_SIDE.firstChild){
				THE_RIGHT_SIDE.removeChild(THE_RIGHT_SIDE.firstChild);
			}
		}//end deleteFaces()

		var theBody = document.getElementsByTagName("body")[0];
		THE_LEFT_SIDE = document.getElementById("leftSide");
		THE_RIGHT_SIDE = document.getElementById("rightSide");
		//Event handling
		THE_LEFT_SIDE.lastChild.onclick = function goToNextLevel() {
			alert("You clicked on correct face.");
			deleteFaces();
			generatefaces();
		};
		theBody.onclick = function gameOver() {
			alert("Game Over!!");
			theBody.onclick = null;
			THE_LEFT_SIDE.lastChild.onclick = null;
		};
	</script>
</head>
<body onload="generateFaces()">
	<div id="leftSide"></div>
	<div id="rightSide"></div>
</body>
</html>
&#13;
&#13;
&#13;

我是Javascript的初学者,在做作业时遇到了问题。 这是事实匹配游戏,您必须单击左侧的额外面部并继续游戏,直到您点击其他地方而不是左侧的额外面部。 上面是HTML文件,JS代码嵌入其中。

我收到的错误为:Uncaught TypeError: Cannot read property 'lastChild' of null第48行,onclick lastChildTHE_LEFT_SIDE的{​​{1}}事件处理函数。我不知道它是怎么来的,我的代码有什么问题? 任何线索都将受到赞赏。

1 个答案:

答案 0 :(得分:2)

您尝试在将ID添加到DOM之前访问ID为leftSide的元素。

如果您将脚本移动到页面正文的末尾,您将不会再看到此错误,而是看到另一个不相关的错误:

<!DOCTYPE html>
<html>
<head>
	<title>Face Matching Game</title>
	<h2>Matching Game</h2>
	<p>Click on the EXTRA face on the left side</p>
	<style type="text/css">
		div { position: absolute; width: 640px; height: 550px; background-color: maroon }
		img { position: absolute; width: 100px; height: 100px}
		#rightSide { left: 650px; border-left: 2px solid black; }
	</style>
 
</head>
<body onload="generateFaces()">
	<div id="leftSide"></div>
	<div id="rightSide"></div>

   	    <script type="text/javascript">
		var NUM_OF_FACES = 0;
		var THE_LEFT_SIDE, THE_RIGHT_SIDE;
		//Function to generate faces
		function generateFaces() {
			THE_LEFT_SIDE = document.getElementById("leftSide");
			THE_RIGHT_SIDE = document.getElementById("rightSide");
			NUM_OF_FACES += 5;
			var image;
			while(NUM_OF_FACES>0){
				image = document.createElement("img");
				image.src = "smile.png";
				image.style.top = Math.floor(Math.random()* (THE_LEFT_SIDE.offsetHeight - 100)) + "px";
				image.style.left = Math.floor(Math.random()* (THE_LEFT_SIDE.offsetWidth - 100)) + "px";
				THE_LEFT_SIDE.appendChild(image);
				NUM_OF_FACES -= 1;
				//console.log(image.style.top, image.style.left )
			}
			var leftSideImages = THE_LEFT_SIDE.cloneNode(true);
			leftSideImages.removeChild(leftSideImages.lastChild);
			THE_RIGHT_SIDE.appendChild(leftSideImages);
		}//end generatefaces()

		function deleteFaces(argument) {
			while(THE_LEFT_SIDE.firstChild){
				THE_LEFT_SIDE.removeChild(THE_LEFT_SIDE.firstChild);
			}
			while(THE_RIGHT_SIDE.firstChild){
				THE_RIGHT_SIDE.removeChild(THE_RIGHT_SIDE.firstChild);
			}
		}//end deleteFaces()

		var theBody = document.getElementsByTagName("body")[0];
		THE_LEFT_SIDE = document.getElementById("leftSide");
		THE_RIGHT_SIDE = document.getElementById("rightSide");
		//Event handling
		THE_LEFT_SIDE.lastChild.onclick = function goToNextLevel() {
			alert("You clicked on correct face.");
			deleteFaces();
			generatefaces();
		};
		theBody.onclick = function gameOver() {
			alert("Game Over!!");
			theBody.onclick = null;
			THE_LEFT_SIDE.lastChild.onclick = null;
		};
	</script>
</body>
</html>