它不显示在浏览器上的任何图像,我不知道它在创建图像错在何处?标识由一个变量imgFace在哪里存储图像,并为它的正确属性,但它似乎没有显示任何东西。的createElement IMG与JavaScript
<!DOCTYPE html>
<html>
<head>
<title>Matching Game</title>
<style>
img {position:absolute;}
div {position:absolute; width:500px; height:500px;}
#RightSide { left: 500px;
border-left: 1px solid black }
</style>
<script>
var numberOfFaces = 5;
var theLeftSide = document.getElementById("LeftSide");
function generateFaces() {
var count = 0;
while(count <= numberOfFaces){
var imgFace = document.createElement("img");
img.src="http://home.cse.ust.hk/~rossiter/mooc/matching_game/smile.png";
var random_nr = Math.random() * 400;
random_nr = Math.floor(random_nr);
imgFace.style.top = random_nr + "px";
imgFace.style.left = random_nr + "px";
theLeftSide.appendChild(imgFace);
count++;
}
}
</script>
</head>
<body onload="generateFaces()">
<h1>Matching Game</h1>
<p>Click on the extra smiling face on the left</p>
<div id="LeftSide">
</div>
<div id="RightSide">
</div>
</body>
</html>
imgFace <=> IMG? – Sebastian
正如塞巴斯蒂安建议的:'img.src =“http://home.cse.ust.hk/~rossiter/mooc/matching_game/smile.png”;'应该是'imgFace.src =“http:// home。 cse.ust.hk/~rossiter/mooc/matching_game/smile.png“;' –
仍然没有工作寿。 –