简体   繁体   English

如何解决以下JS错误?

[英]How to resolve the following JS error?

I am writing a simple html + js script which has a few images on left sides and one less on right side and on clicking a correct element(last child of the div) adds the images on both right and left sides(+5 everytime). 我正在编写一个简单的html + js脚本,该脚本的左侧有一些图像,右侧少了一些,单击正确的元素(div的最后一个子元素)会在右侧和左侧同时添加图像(每次+5) 。 Here's the code: 这是代码:

<html>
    <head>
    <style type="text/css">
        img {
            position:absolute;
        }

        div {
            position:absolute;
            width:500px; 
            height:500px;
        }

        #rightSide { left: 500px; 
            border-left: 1px solid black; 
        }


    </style>

    </head>
    <body onload="generateFaces()">
        <h1>Matching Game</h1>
        <div id="leftSide"></div>
        <div id="rightSide"></div>
        <script>
            var numberOfFaces = 5;
            var theLeftSide = document.getElementById("leftSide");
            generateFaces(theLeftSide);
            function generateFaces(theLeftSide) {
                for (var i = 0; i < numberOfFaces; i++) {
                var img = document.createElement("IMG");
                img.src="smile.png";
                img.style.top = Math.floor(Math.random() * 401);
                img.style.height = 100;
                img.style.left = Math.floor(Math.random() * 401);
                theLeftSide.appendChild(img);
            };

        }

            leftSideImages = theLeftSide.cloneNode(true);
            var theRightSide = document.getElementById("rightSide");
            leftSideImages.removeChild(leftSideImages.lastChild);
            theRightSide.appendChild(leftSideImages);

            var theBody = document.getElementsByTagName("body")[0];
            theLeftSide.lastChild.onclick= function nextLevel(event){ 
                theLeftSide.innerHTML='';
                theRightSide.innerHTML='';
                event.stopPropagation(); 
                numberOfFaces += 5; 
                generateFaces(); 

            /*  while(theLeftSide.firstChild)
                {
                    theLeftSide.removeChild(theLeftSide.firstChild);
                }
                while(theRightSide.firstChild)
                {
                    theRightSide.removeChild(theRightSide.firstChild);
                }*/1

            };
            theBody.onclick = function gameOver() {
                alert("Game Over!");
                theBody.onclick = null;
                theLeftSide.lastChild.onclick = null;
                }; 

        </script>



        </script>
    </body>
</html>

However it's giving me an error: 但是,这给了我一个错误:

Uncaught TypeError: Cannot read property 'appendChild' of undefined

Also I have written code for deleting the images, this is also not working. 我也写了删除图像的代码,这也不起作用。

Try this: 尝试这个:

<!DOCTYPE html>
<html>
<head>
<style type="text/css">
    /*img {
        position:absolute;
    }*/

    div {
        position:absolute;
        width:500px; 
        height:500px;
    }

    #rightSide { left: 500px; 
        border-left: 1px solid black; 
    }


</style>

</head>
<body>
    <h1>Matching Game</h1>
    <div id="leftSide"></div>
    <div id="rightSide"></div>
    <script src="jquery.js"></script>
    <script>
        var numberOfFaces = 5;
        var theLeftSide = document.getElementById("leftSide");
        generateFaces();
        function generateFaces() {
            for (var i = 0; i < numberOfFaces; i++) {
                (function(i) {
                    var img = document.createElement("img");
                    img.src="smile.png";
                    // img.style.top = Math.floor(Math.random() * 401);
                    img.style.height = 100;
                    // img.style.left = Math.floor(Math.random() * 401);
                    console.log('img');
                    theLeftSide.appendChild(img);
                }(i));
                console.log("running");

            };
        }

        leftSideImages = theLeftSide.cloneNode(true);
        var theRightSide = document.getElementById("rightSide");
        leftSideImages.removeChild(leftSideImages.lastChild);
        theRightSide.appendChild(leftSideImages);

        var theBody = document.getElementsByTagName("body")[0];
        theLeftSide.lastChild.onclick= function nextLevel(event){ 
            theLeftSide.innerHTML='';
            theRightSide.innerHTML='';
            event.stopPropagation(); 
            numberOfFaces += 5; 
            generateFaces(); 

          while(theLeftSide.firstChild)
            {
                theLeftSide.removeChild(theLeftSide.firstChild);
            }
            while(theRightSide.firstChild)
            {
                theRightSide.removeChild(theRightSide.firstChild);
            }

        };
        theBody.onclick = function gameOver() {
            alert("Game Over!");
            theBody.onclick = null;
            theLeftSide.lastChild.onclick = null;
            }; 

    </script>
</body>
</html>

setting the images to absolute would put the images over eachother 将图像设置为absolute会使图像彼此重叠

change your function parameter name 更改您的功能参数名称

<html>
    <head>
    <style type="text/css">
        img {
            position:absolute;
        }

        div {
            position:absolute;
            width:500px; 
            height:500px;
        }

        #rightSide { left: 500px; 
            border-left: 1px solid black; 
        }


    </style>

    </head>
    <body onload="generateFaces()">
        <h1>Matching Game</h1>
        <div id="leftSide"></div>
        <div id="rightSide"></div>
        <script>
            var numberOfFaces = 5;
            var theLeftSide = document.getElementById("leftSide");
            generateFaces(theLeftSide);
            function generateFaces(the_Lef_tSide) {
                for (var i = 0; i < numberOfFaces; i++) {
                var img = document.createElement("IMG");
                img.src="smile.png";
                img.style.top = Math.floor(Math.random() * 401);
                img.style.height = 100;
                img.style.left = Math.floor(Math.random() * 401);
                theLeftSide.appendChild(img);
            };

        }

            leftSideImages = theLeftSide.cloneNode(true);
            var theRightSide = document.getElementById("rightSide");
            leftSideImages.removeChild(leftSideImages.lastChild);
            theRightSide.appendChild(leftSideImages);

            var theBody = document.getElementsByTagName("body")[0];
            theLeftSide.lastChild.onclick= function nextLevel(event){ 
                theLeftSide.innerHTML='';
                theRightSide.innerHTML='';
                event.stopPropagation(); 
                numberOfFaces += 5; 
                generateFaces(); 

            /*  while(theLeftSide.firstChild)
                {
                    theLeftSide.removeChild(theLeftSide.firstChild);
                }
                while(theRightSide.firstChild)
                {
                    theRightSide.removeChild(theRightSide.firstChild);
                }*/1

            };
            theBody.onclick = function gameOver() {
                alert("Game Over!");
                theBody.onclick = null;
                theLeftSide.lastChild.onclick = null;
                }; 

        </script>



        </script>
    </body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何解决错误 TypeError: x is not a function for testing the following function in Jest? - How to resolve the error TypeError: x is not a function for testing of the following function in Jest? 为什么在下面的代码中出现循环引用错误? 以及如何解决? - Why circular reference error is occured in following code? and how to resolve it? 如何解决 Fetch API js 中的 CORS 错误 - How to resolve CORS error in Fetch API js 运行应用程序时如何解决以下错误:无法解析所有文件进行配置? - How can i solve the following error while run the app : Could not resolve all files for configuration? 在运行Jest进行测试时,如何解决以下编译错误? - How can I resolve the following compilation error when running Jest for testing? Three.js-如何解决Eclipse中删除时的语法错误 - Three.js - how to resolve syntax error on delete in Eclipse 如何解决 NODE.Js HTTP POST“ECONNRESET”错误 - How to resolve NODE.Js HTTP POST "ECONNRESET" Error 如何解决 html/js 计算器不是数字 (NaN) 错误? - How to resolve html/js Calculator Not A Number (NaN) Error? 如何使用 redux 解决 React js 中的 401 错误? - How to resolve 401 error in react js using redux.? 如何解决反应 js 中 JSON 的反应导出错误? - How to resolve react export error for JSON in react js?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM