使用javascript创建元素img

使用javascript创建元素img,javascript,image,createelement,Javascript,Image,Createelement,它在浏览器上没有显示任何图像,我想知道它在创建图像时哪里出错了?我制作了一个变量imgFace,我在其中存储图像并为其创建正确的属性,但它似乎没有显示任何内容 <!DOCTYPE html> <html> <head> <title>Matching Game</title> <style> img {position:absolute;} div {position:abso

它在浏览器上没有显示任何图像,我想知道它在创建图像时哪里出错了?我制作了一个变量imgFace,我在其中存储图像并为其创建正确的属性,但它似乎没有显示任何内容

<!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>

配对游戏
img{位置:绝对;}
div{位置:绝对;宽度:500px;高度:500px;}
#右侧{左侧:500px;
左边框:1px纯黑}
var numberOfFaces=5;
var theLeftSide=document.getElementById(“LeftSide”);
函数生成器(){
var计数=0;
while(count
function generateFaces(){
var计数=0;
而(计数替换此行

img.src="http://home.cse.ust.hk/~rossiter/mooc/matching_game/smile.png";

注意:
img
不是您定义的正确变量名。它应该是
imgFace

两件事

1) 您有
img.src
,它应该是
imgFace.src

2) 您没有调用
generateFaces
函数


正如塞巴斯蒂安所建议的那样,
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”;
仍然无法工作。
在加载时调用generateFaces
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";